[thelist] JavaScript validation for alphanumeric

Matthias Ritzkowski mritzkowski at groundwateranalytical.com
Tue Oct 15 16:44:16 CDT 2002


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.

Windows 2000 client (code works on IIS Server, but not on Suse 7.2 Linux)
<-- this really should not matter, should it??

tia
Matthias


snip--


function ValidateForm(){

	var sizechar = 6;			//length for password
	var upassID=document.myForm.UPASS;

	//check if passsword is filled out
	if ((upassID.value==null)||(upassID.value=="")){
		alert("Please enter your password");
		upassID.focus();
		return false;
	}
//add regex alphanumeric check here
			if (upassID.length > sizechar) {
        alert('Your password is too long');
		upassID.focus();
        return false;
    }
    if (upassID.length < sizechar) {
        alert('Your password is too short');
		upassID.focus();
        return false;
    }
		var upass_string = upassID.value;

    var
valid="123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    for (var i=0; i<sizechar; i++) {
        if (valid.indexOf(upass_string.charAt(i)) < 0) {
            alert('Your password contains invalid characters');
		upassID.focus();
            return false;
        }
    }

	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;
 }

//-->
</Xcript>






More information about the thelist mailing list