0

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" .

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.