Checking Password onsubmit

喜欢而已 提交于 2019-12-11 06:38:37

问题


I have a basic signup form:

<form action="adduser" method="post" onsubmit='passwordCheck()'>
    Email Address: <input type="email" name="email" required autocomplete="on" placeholder="fr@star.com"/><br/>
    Username: <input type="text" name="username" maxlength=25 required placeholder="JoyfulSophia"/><br/>
    Password: <input type="password" name="password" id='password' onkeydown='checkPassword()' maxlength=30 required placeholder="**********" />
    Password (Again): <input type='password' id='pass_repeat'/>
    <br/>
    <br/>
    <input type="submit" value="Send" /> <input type="reset">
</form>

So when the form is submitted, it calls passwordCheck(), which tests it against easily guessable passwords, not repeating the same password etc. It puts the error, if any, in a <p> (not shown). checkPassword() works and the error is displaying correctly, but the form still submits while JS docs that I've read say that if onsubmit returns false, the form should not submit.

Why is the form submitting when checkPassword() returns false? Also, checkPassword() is not being called on each keystroke in the passwords input.


回答1:


It needs to be

<form ... onsubmit="return passwordCheck();">

as for the onkeydown, check that your checkPassword() function is actually being called. have it do an alert() or something similar.




回答2:


Add return

onsubmit='return passwordCheck()'


来源:https://stackoverflow.com/questions/8218325/checking-password-onsubmit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!