[thelist] JavaScript validation for alphanumeric

Syed Zeeshan Haider szh at softhome.net
Thu Oct 17 10:22:01 CDT 2002


From: "Matthias Ritzkowski"
To: <thelist at lists.evolt.org>
Sent: October 16, 2002, Wednesday 2:43 AM
Subject: [thelist] JavaScript validation for alphanumeric


> Hi all -
>
> Matthias here I just rejoined with a different email address because we
> changed our mail server. Here's my question for (maybe) regexp experts:
> I need a client side JavaScript validation for a 6 character alphanumeric
> password
>
> e.g.. "water1" is ok
> "waters" is not ok
> "123456" is not ok
>
> for some reason I have the code below working in one place, but cannot seem
> to work it in another.

I am not any expert of regexp but after some work out on your script I have
found that following part of your script is creating problem.

<problem script>

var num_valid="123456789"

    for (var i=0; i<sizechar; i++) {
        if (num_valid.indexOf(upass_string.charAt(i)) < 0) {
            alert('Your password contains only characters. Please enter an
alphanumeric value like -alpha1-');
upassID.focus();
            return false;
        }
    }

var
alph_valid="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

    for (var i=0; i<sizechar; i++) {
        if (alph_valid.indexOf(upass_string.charAt(i)) < 0) {
            alert('Your password contains only numbers. Please enter an
alphanumeric value like -alpha1-');
upassID.focus();
            return false;
        }
    }



return true;
 }

</problem script>

YOU MUST consider replacing above script with following one. I have amended
above script into following.

<amended script>

 var alphaCount=0
 var numCount=0

var num_valid="123456789"

    for (var i=0; i<sizechar; i++) {
        if (num_valid.indexOf(upass_string.charAt(i)) < 0) {
         numCount++
        }
    }
 if(numCount==upass_string.length){
 alert('Your password contains only characters. Please enter an alphanumeric
value like -alpha1-');
upassID.focus();
            return false;
   }

var
alph_valid="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

    for (var i=0; i<sizechar; i++) {
        if (alph_valid.indexOf(upass_string.charAt(i)) < 0) {
            alphaCount++
        }
    }
 if(alphaCount==upass_string.length){
 alert('Your password contains only numbers. Please enter an alphanumeric value
like -alpha1-');
upassID.focus();
            return false;
   }

return true;
 }

</amended script>

Hope this helps!

Syed Zeeshan Haider.
http://syedzeeshanhaider.faithweb.com/





More information about the thelist mailing list