How to validate a value against a regular expression in server side?

Raja
Posted by Raja under C# category on | Views : 6274
To validate a value against a regular expression in server side, we can use RegexStringValidator class that is available in System.Configuration namespace.

Following code snippet will validate an email id entered into txtEmail TextBox.

RegexStringValidator r = new RegexStringValidator(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
try
{
r.Validate(txtEmail.Value);
args.IsValid = true;
}
catch (ArgumentException ee)
{
lblMessage.Text = "Error occured: Doesn't seem to be valid email id. <br />" +
ee.Message.ToString();
args.IsValid = false;
}

Comments or Responses

Login to post response