I tried to validate the password with a regex function in javascript. I set the button disabled on default and when the password is valid, I want to enable the button.
The validation doesn't work.
Here you can see the javascript code:
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)
}
Here is the html form
<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>
Error Message:
registration:97 Uncaught ReferenceError: validPW is not defined
The validation works perfectly in my tests, although the function you have posted is missing 2 closing braces, so it is technically not valid JavaScript, though I assume the braces are present in your local copy. Here's what it should be:
function validPW() { if (isValid("passwort",document.getElementById("passwordRegistrationID").value)) { document.getElementById('passwordRegistrationID').disabled=false; } function isValid (type, toValidate){ switch (type){ case "grossbuchstabeVorhanden" : var grossbuchstabe = /[AZ]+/; return grossbuchstabe.test (toValidate); break; case "kleinbuchstabenVorhanden" : var kleinbuchstabe = /[az]+/; 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) } } }
Otherwise, I couldn't find any issues whilst doing a quick test with a valid and and invalid password.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.