Thursday, February 27, 2014

Regular Expressions for Validation.

This is regular expression using for validation the website , Ip address , Frist level Domain, Second level Domain, Email validaiton expression and phone number expression validation.

bool isEmail = Regex.IsMatch(emailString, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z");


$regexp = "^(https?://)"; // http://
$regexp .= "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?"; //
usernamePassword
$regexp .= "(([0-9]{1,3}\.){3}[0-9]{1,3}"; // IP- 199.194.52.184
$regexp .= "|"; // allows either IP or domain
$regexp .= "([0-9a-z_!~*'()-]+\.)*"; // tertiary domain(s)- www.
$regexp .= "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\."; // second level domain
$regexp .= "[a-z]{2,6})"; // first level domain- .com or .museum
$regexp .= "(:[0-9]{1,4})?"; // port number- :80
$regexp .= "((/?)|"; // a slash isn't required if there is no file name
$regexp .= "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
$regexp .= @"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$"; //Phone number
$regexp .= @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z"; // email validation

No comments:

Post a Comment