1

I´m using C# and ASP.NET for developing in Windows Azure.

I want to do a global Method to validate fields, my idea is to do a global Method like (for example in Site.Master)

static Regex dateRegex = new Regex("^\\d{2}/\\d{2}/\\d{4}$");
public boolean validate( string type, string stringToValdate)
{
  boolean valid = NO;
  if (type = "date")
  {
      Match m = fechaRegex.Match(stringToValdate);
      if(m.Success)
      {
            valid = true;
      }
  }

 return valid;
}

And then use it in another webform

using ????

Site.Master.validate("date",TextBox1.Text);
2
  • Why don't you use the built-in validations provided by the framework? Note that for dates you should expect users to enter them in their locale, which will not work well with your validation (apart from accepting bogus dates such as 99/99/9999 as well). Global static stuff is a strong code smell btw., you should avoid that and use more suitable techniques. Commented Sep 8, 2012 at 18:56
  • 2
    If you are going to use static/global regex patterns, you should consider using the "compiled" flag .. so they are only compiled the once. For example new Regex("^\\d{2}/\\d{2}/\\d{4}$", RegexOptions.Compiled); Commented Sep 8, 2012 at 19:44

3 Answers 3

2

I would introduce my own custom static Validation class instead of making it a function in the Global.asax - that's more for global site configuration.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use Extension Method on Master Type

public static class Extension
{ 
    public static boolean Validate(this Master master, 
                                   string type, 
                                   string stringToValdate)
    {
      boolean valid = NO;
      if (type = "date")
      {
          Match m = fechaRegex.Match(stringToValdate);
          if(m.Success)
          {
                valid = true;
          }
      }

     return valid;
    }
}

Use Case :

using NamesPaceOfExtension;

Site.Master.Validate("date",TextBox1.Text);

6 Comments

I don´t undestand this part this Master master . and how can i invoke the method from other webforms? for example in WebRole1.WebFormsProducts
If you create extension method on Master type, you can call this method from all pages, just add namespace of your extensions
There's a typo. It should be this Master master, string type. The "this" here in the parameter list marks the "extension method" thing. This is not a method that is an "extension", it's a "extension method":) see msdn.microsoft.com/en-us/library/bb383977.aspx
THANKS Candie! your help was useful!
I'am happy to help you vlopezla
|
0

You could use a static class to put all your validations in 1 place, for example:

public static class Validation
{
  private static Regex dateRegex = new Regex("^\\d{2}/\\d{2}/\\d{4}$");

  public static boolean Validate(string type, string stringToValdate)
  {
    boolean valid = false;
    if (type = "date")
    {
        Match m = dateRegex.Match(stringToValdate);
        if(m.Success)
        {
          valid = true;
        }
    }

   return valid;
  }
}

Then you can call this from anywhere:

bool valid = Validation.Validate("date", "01/01/2012");

However, static classes and methods are convenient and easy, but harder to test. If you are doing a lot of unit testing, it is often easier to make a non-static class to do validation that implements some interface, and pass one in to every page request using a dependency injection framework. That is a bit more advanced of a topic though.


Also, unless you have to, you shouldn't really make 1 big "validate anything and everything" method, passing in the type as a string. It is against separation of concerns and single-responsibility principal. It is better to make separate methods for each type.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.