javascript - Why validation for password field validation not working? -
i'm new web programming can please tell me what's wrong following code?
<!doctype html> <html> <head> <title>form validation</title> <script type="text/javascript"> function validate (form) { // valriable declaration var returnvalue = true; var username = form.txtusername.value; var password1 = form.txtpassword.value; var password2 = form.txtpassword2.value; // check username length if (username.length < 6) { returnvalue = false; alert("your username must @ least\n6 characters long.\nplease try again."); frmregister.txtusername.focus(); }; // check password length if (password1.length < 6) { returnvalue = false; alert("your password must @ least\n6 characters long.\nplease try again."); frmregister.txtpassword.value = ""; frmregister.txtpassword2.value = ""; frmregister.txtpassword.focus(); }; // check match of password field if (password1.value != password2.value) { returnvalue = false; alert("your password entries did not match.\nplease try again."); frmregister.txtpassword.value = ""; frmregister.txtpassword2.value = ""; frmregister.txtpassword.focus(); }; return returnvalue; } </script> </head> <body> <form method="post" name="frmregister" action="register.html" onsubmit="return validate(this);"> <div><label for="txtusername">username : </label> <input type="text" name="txtusername" id="txtusername" size="12" /> </div> <div><label for="txtpassword">password : </label> <input type="text" name="txtpassword" id="txtpassword" size="12" /> </div> <div> <label for="txtpassword2">confirm password : </label> <input type="text" name="txtpassword2" id="txtpassword2" size="12" /> </div> <div> <input type="submit" value="log in" /> </div> </form> </body> </html>
first of stop using return event handler
. convert code <form ... onsubmit="validate(event,this)">
change function validate(event,form)
;
wherever feel form should not submitted.. write :
event.preventdefault()
instead of return false
demonstration : http://codepen.io/anon/pen/kgmel
Comments
Post a Comment