In web form, I have multiple fields and each field have some unique validation like phone and zip code have only number and some of the fields do not allow special characters. So how do I validate each field?
Now I validating like
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 32 && (charCode < 48 || charCode > 57)||(charCode==32))
return false;
return true;
}
function isAlphaNumericKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 32 && (charCode < 65 || charCode > 90) && (charCode < 97 || charCode > 122)&& (charCode < 48 || charCode > 57))
return false;
return true;
}
and some cases I need to allow some special characters like
function isANhypenKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 32 && (charCode < 65 || charCode > 90) && (charCode < 97 || charCode > 122)&& (charCode < 48 || charCode > 57) && (charCode!=44) && (charCode!=45))
return false;
return true;
}
and HTML form
<input type="text" name="zipcode" id="zipcode" onkeypress="return isNumberKey(event);"/>
<input type="text" name="shipname" id="shipname" onkeypress="return isANhypenKey(event);" />
How to reduce my JS code. Can I get any JS library file for this or JS function can solve this? Thanks