Alphanumeric Regular Expression in java script

We will validate text box data to be alphanumeric data onblur event. On blur we Validate_Alphanumeric js event will be called

<asp:TextBox ID=”txt” runat=”server” onblur=”javascript:Validate_AlphaNumeric(this.id);” MaxLength=”100″></asp:TextBox>

function Validate_AlphaNumeric(id) {
var regEx = /^([a-zA-Z0-9_-]+)$/;
var strValue = document.getElementById(id).value;
if (regEx.test(strValue)) {
alert(“ Value is alphanumeric”);
}
else {
alert(“ Value is not an alphanumeric”);
}

}

 regular expression string should be in between / & /
^         start of string
a-zA-Z0-9_-  a or b or c or … z or 0 or 1 or … 9 or _ or –
$         end of string

Happy Koooding! Hope this helps

Advertisements