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);
Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389
vlopezla
  • 95
  • 1
  • 15
  • 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. – Lucero Sep 08 '12 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);` – dano Sep 08 '12 at 19:44

3 Answers3

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.

James
  • 80,725
  • 18
  • 167
  • 237
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);
Kjartan
  • 18,591
  • 15
  • 71
  • 96
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
  • I don´t undestand this part `this Master master `. and how can i invoke the method from other webforms? for example in WebRole1.WebFormsProducts – vlopezla Sep 08 '12 at 18:39
  • If you create extension method on Master type, you can call this method from all pages, just add namespace of your extensions – Aghilas Yakoub Sep 08 '12 at 18:40
  • 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 http://msdn.microsoft.com/en-us/library/bb383977.aspx – quetzalcoatl Sep 08 '12 at 18:40
  • THANKS Candie! your help was useful! – vlopezla Sep 08 '12 at 19:00
  • I'am happy to help you vlopezla – Aghilas Yakoub Sep 08 '12 at 19:01
  • For me this wouldn't be the correct approach. This code is not specific to the `Master` page. It would be better placed in it's own class which can be used on various pages e.g. `MyValidationClass.Validate("date", TextBox1.Text);` – James Sep 10 '12 at 07:34
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.

CodingWithSpike
  • 42,906
  • 18
  • 101
  • 138