CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Using RegexStringValidator

1600 Views
Copy Code Show/Hide Line Numbers
   1:  using System;
   2:  using System.Configuration;
   3:   
   4:  namespace Microsoft.Samples.AspNet.Validators
   5:  {
   6:    class UsingRegexStringValidator
   7:    {
   8:      static void Main(string[] args)
   9:      {
  10:        // Display title.
  11:        Console.WriteLine("ASP.NET Validators");
  12:        Console.WriteLine();
  13:   
  14:        // Create RegexString and Validator.
  15:        string testString = "someone@example.com";
  16:        string regexString = @"^[a-zA-Z\.\-_]+@([a-zA-Z\.\-_]+\.)+[a-zA-Z]{2,4}$";
  17:   
  18:        RegexStringValidator myRegexValidator = new RegexStringValidator(regexString);
  19:   
  20:        // Determine if the object to validate can be validated.
  21:        Console.WriteLine("CanValidate: {0}", 
  22:                          myRegexValidator.CanValidate(testString.GetType()));
  23:   
  24:        try
  25:        {
  26:          // Attempt validation.
  27:          myRegexValidator.Validate(testString);
  28:          Console.WriteLine("Validated.");
  29:        }
  30:        catch (ArgumentException e)
  31:        {
  32:          // Validation failed.
  33:          Console.WriteLine("Error: {0}", e.Message.ToString());
  34:        }
  35:   
  36:        // Display and wait
  37:        Console.ReadLine();
  38:      }
  39:    }
  40:  }
by Dean Weber
  October 07, 2009 @ 7:01am
Tags:

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate