我尝试在javascript中使用正则表达式函数验证密码。我在默认情况下将按钮设置为禁用,当密码有效时,我想启用按钮。
验证不起作用。
在这里你可以看到javascript代码:
function validPW() {
if (isValid("passwort",document.getElementById("passwordRegistrationID").value)) {
document.getElementById('passwordRegistrationID').disabled=false;
}
function isValid (type, toValidate){
switch (type){
case "grossbuchstabeVorhanden" :
var grossbuchstabe = /[A-Z]+/;
return grossbuchstabe.test (toValidate);
break;
case "kleinbuchstabenVorhanden" :
var kleinbuchstabe = /[a-z]+/;
return kleinbuchstabe.test (toValidate);
case "zahlenVorhanden" :
var zahlen = /[0-9]+/;
return zahlen.test (toValidate);
case "sonderzeichenVorhanden" :
var sonderzeichen = /[?!$%&/=<>_]+/;
return sonderzeichen.test (toValidate);
case "laengeRjichtig" :
var laenge =/.{9,}/;
return laenge.test (toValidate);
case "passwort":
return isValid("grossbuchstabeVorhanden",toValidate) && isValid("kleinbuchstabenVorhanden",toValidate)
&& isValid("zahlenVorhanden",toValidate) && isValid("sonderzeichenVorhanden",toValidate)
&& isValid("laengeRjichtig",toValidate)
}
这是html表单
<form role="form" action="/user/doRegistration" method="post" class="login-form">
<div class="form-group" id="usernameRegistrationDiv">
<label class="sr-only" for="form-usernameRegistration">Username</label>
<input type="email" name="form-usernameRegistration" required="required" placeholder="Username..." class="form-usernameRegistration form-control"
id="form-usernameRegistration">
</div>
<div class="form-group" id="passwordRegistrationDiv">
<label class="sr-only" for="form-passwordRegistration">Password</label>
<input type="password" name="form-passwordRegistration" required="required" placeholder="Password..." class="form-passwordRegistration form-control"
id="passwordRegistrationID" oninput="validPW();">
</div>
<div class="form-group">
<label class="sr-only" for="form-password-registration">Password repeat</label>
<input type="password" name="form-password-registration" required="required" placeholder="Password repeat..." class="form-password-registration form-control"
id="form-password">
</div>
<button type="submit" id="buttonRegistration" class="btn" disabled> Sign up!</button>
</form>
错误讯息:
registration:97 Uncaught ReferenceError: validPW is not defined
答案 0 :(得分:0)
验证在我的测试中完美运行,虽然你发布的功能缺少2个结束括号,所以它在技术上是无效的JavaScript,但我认为大括号存在于你的本地副本中。这是它应该是什么:
function validPW() {
if (isValid("passwort",document.getElementById("passwordRegistrationID").value)) {
document.getElementById('passwordRegistrationID').disabled=false;
}
function isValid (type, toValidate){
switch (type){
case "grossbuchstabeVorhanden" :
var grossbuchstabe = /[A-Z]+/;
return grossbuchstabe.test (toValidate);
break;
case "kleinbuchstabenVorhanden" :
var kleinbuchstabe = /[a-z]+/;
return kleinbuchstabe.test (toValidate);
case "zahlenVorhanden" :
var zahlen = /[0-9]+/;
return zahlen.test (toValidate);
case "sonderzeichenVorhanden" :
var sonderzeichen = /[?!$%&/=<>_]+/;
return sonderzeichen.test (toValidate);
case "laengeRjichtig" :
var laenge =/.{9,}/;
return laenge.test (toValidate);
case "passwort":
return isValid("grossbuchstabeVorhanden",toValidate) && isValid("kleinbuchstabenVorhanden",toValidate)
&& isValid("zahlenVorhanden",toValidate) && isValid("sonderzeichenVorhanden",toValidate)
&& isValid("laengeRjichtig",toValidate)
}
}
}
&#13;
否则,在使用有效且无效的密码进行快速测试时,我无法发现任何问题。