How to validate a value against a regular expression in Client Side (JavaScript)?

Posted by Raja under C# category on | Views : 4390
To validate a value against regular expression in client side using JavaScript, use following function.

Notice that in the server side, we write regular expression in double quotes to validate, in client side javascript, we write in between / and /. Notice the value of emailExpression in the following function, it is prefixed and suffixed with / and /.

        function ValidateEmail()
var emailExpression = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;

var element = document.getElementById('<%=txtFirstName.ClientID %>');

Above function is assuming that you have a txtFirstName TextBox for which the value needs to be validated. If that value will have the correct email id format, this will return true else false.


Comments or Responses

Login to post response