Friday, February 20, 2009

JavaScript Validation: Alphanumeric and underscore only


//add this to textbox :onkeypress="validate(event);"
// validates and don't allow typing characters other than a-z,A-Z, 0-9 and _
function validatePortalName(e)
{
if(window.event) // IE
keynum = e.keyCode;
else if(e.which) // Netscape/Firefox/Opera
keynum = e.which;
return ((keynum >= 97 && keynum <=122)(keynum >= 65 && keynum <=90)(keynum>=48 && keynum<=57) keynum==95);
}

Note: code is tested in ie7 and mozilla firefox 3.0.6. Please test in the borwser you target before using it in production. regards

No comments:

Post a Comment

Please add your valuable comments about this post if it helped you. Thanks

Popular Posts