What's the best way in Javascript to validate that a field is alphanumeric?
Printable View
What's the best way in Javascript to validate that a field is alphanumeric?
Code:<script type="text/javascript">
//now tested:
var str = "skslkdrt492"
regexp1 = /\d/
regexp2 = /\w/
bool = true
for (i=0; i<str.length; i++)
{
if (str.charAt(i).search(regexp1) == -1 && str.charAt(i).search(regexp2) == -1)
{
bool = false
}
}
if (bool == false)
{
//not alpha-numeric.
}
</script>