General Regular Expression Class

This is a regular expression class for handling most of the validation. I have created this for my project use. All general validations like check for emails, empty, isnumeric have been included in this Class.

A Small Snippet from the Class is here for your reference...


public static bool IsNumber(string str)
{
Regex objNotNumberPattern = new Regex("[^0-9.-]");
Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");

return !objNotNumberPattern.IsMatch(str) &&
!objTwoDotPattern.IsMatch(str) &&
!objTwoMinusPattern.IsMatch(str) &&
objNumberPattern.IsMatch(str);
}


public static bool IsValidEmail(string email)
{
//regular expression pattern for valid email
//addresses, allows for the following domains:
//com,edu,info,gov,int,mil,net,org,biz,name,museum,coop,aero,pro,tv
string pattern = @"^[-a-zA-Z0-9][-.a-zA-Z0-9]*@[-.a-zA-Z0-9]+(\.[-.a-zA-Z0-9]+)*\.
(com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|tv|[a-zA-Z]{2})$";
//Regular expression object
Regex check = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);
//boolean variable to return to calling method
bool valid = false;

//make sure an email address was provided
if (string.IsNullOrEmpty(email))
{
valid = false;
}
else
{
//use IsMatch to validate the address
valid = check.IsMatch(email);
}
//return the value to the calling method
return valid;
}


For whole class you can download the attachment...


Attachments

  • Validation Class (27874-23120-Validation.cs.txt)
  • Comments

    Author: Manindra Kumar Upadhyay09 Apr 2010 Member Level: Gold   Points : 1

    try this link:
    http://www.manindra.net/post/2010/04/09/AspNet-Regular-Expressions.aspx



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: