JSManiac
10-14-2006, 10:58 PM
I want to check whether an input field is alphanumeric. If not the form should not be submitted. Below is the code I am using in my function..
var valid="123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sizechar = document.form.field.value.length;
for (var i=0; i<sizechar; i++) {
if (valid.indexOf(document.form.field.value.charAt(i)) < 0) {
alert('Your password contains invalid characters.');
document.form.field.value.focus();
return false;
}
The problem:
If the password is not alphanumeric I get the alert as expected. However, the form still submits. What am I doing wrong?
Thanks!
var valid="123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sizechar = document.form.field.value.length;
for (var i=0; i<sizechar; i++) {
if (valid.indexOf(document.form.field.value.charAt(i)) < 0) {
alert('Your password contains invalid characters.');
document.form.field.value.focus();
return false;
}
The problem:
If the password is not alphanumeric I get the alert as expected. However, the form still submits. What am I doing wrong?
Thanks!