问题
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