Med Akram Z
Med Akram Z

Reputation: 170

Javascript function to validate password

I wanna validate an input of password

<form name="regist" action="proc/regist.php" onsubmit="return validate_form1()" method="post">
<p>Your Username:   <input type="text" name="regist_name" /></p>
<p>Your Password:   <input type="password" name="regist_password1"/> at least 6 characters</p>
<p>Repeat Password: <input type="password" name="regist_password2" /></p>
<input type="submit" value="Register NOW"> 

i tried to set the function like this :

function validate_form1(){
    if(regist_password1.length<6){
        alert("Your password is too short , please try again!");
        return false;
    }
}

maybe the problem is in "regist_password1.length" .

Upvotes: 0

Views: 1400

Answers (2)

James Allardice
James Allardice

Reputation: 165971

I'm assuming regist_password1 (which is the id of the input element) is not a variable containing the value of the input, which is what it needs to be:

if(document.getElementById("regist_password1").value.length < 6) {
    //Failed
}

In your current code, regist_password1 will be undefined (assuming you haven't declared it as a global variable outside of the validation function.

Upvotes: 2

SLaks
SLaks

Reputation: 887459

You need to give the element an ID, then call document.getElementById('some ID').

Upvotes: 0

Related Questions