java - Javascript won't interact with html code -
the code written in jsp should validate password , confirm password correctness , continue submit event java servlet. problem won't show alert messages nor focusing on password input if fails validation script. i've used chrome, explorer , eclipse default browser.. won't work
function validate(event){ event.preventdefault(); var pattern = /^(?=.*[a-z].*[a-z])(?=.*[a-z])(?=.*\d)[a-za-z\d]{8,32}$/; var pwd = document.form.password.value; var confpwd = document.fomr.confpass.value; if(pwd.match(pattern)){ alert("password must between 8 32 characters,\n have @ least 1 digit \n 2 lower , 1 upper case letters."); document.form.password.focus(); return false; }else if(pwd == confpwd){ alert("passwords not match. please try again."); document.form.password.focus(); document.form.confpass.focus(); return false; }else{ document.form.submit(); } }
<form name="form" method="post" action="registerpath" onsubmit="return validate(event);"> <table class="box"> <tr> <td class="left">password:</td> <td><input type="password" name="password" placeholder="enter password" required/></td> </tr> <tr> <td class="left">confirm password:</td> <td><input type="password" name="confpass" placeholder="re-enter password" required/></td> </tr> </table> </form>
this working code you.
function validate(event) { event.preventdefault(); var pattern = /^(?=.*[a-z].*[a-z])(?=.*[a-z])(?=.*\d)[a-za-z\d]{8,32}$/; var pwd = document.form.password.value; var confpwd = document.form.confpass.value; if (!pwd.match(pattern)) { alert("password must between 8 32 characters,\n have @ least 1 digit \n 2 lower , 1 upper case letters."); document.form.password.focus(); return false; } else if (pwd != confpwd) { alert("passwords not match. please try again."); document.form.password.focus(); document.form.confpass.focus(); return false; } else { document.form.submit(); } }
<form name="form" method="post" action="registerpath" onsubmit="return validate(event);"> <table class="box"> <tr> <td class="left">password:</td> <td><input type="password" name="password" placeholder="enter password" required /></td> </tr> <tr> <td class="left">confirm password:</td> <td><input type="password" name="confpass" placeholder="re-enter password" required /></td> </tr> <tr> <td colspan="2"><button type="submit">submit</button></td> </tr> </table> </form>
Comments
Post a Comment