Abdelouahab Pp
Abdelouahab Pp

Reputation: 4440

HTML5 setCustomValidation fails on Else

i've this code:

function checkPassword(pass1, pass2) {
var p1 = document.getElementById(pass1)
var p2 = document.getElementById(pass2)
if (p1.value != p2.value) {
    p2.setCustomValidity('verifiez que vous avez ecrit le meme mot de passe');
} else {
    p2.setCustomValidity('')
}}

function Banned(password) {
pas = document.getElementById(password)
ban_list = ["111111", "11111111", ...]
for (var i = 0; i < ban_list.length; i++) {
    if (pas.value == ban_list[i]) {
    pas.setCustomValidity("Mot de Passe Facile, Veuillez le changer")
   } else
    {
      pas.setCustomValidity("")}
}}

html:(the even is OnBlur)

<label for="pass1">Votre mot de passe</label>
<input class="keyboardInput" id="pass1" name="pass1" type="password" required onkeypress="checkCapsLock(event);" placeholder="" pattern=".{6,}" title="" onblur="checkPassword('pass1','pass2'); Banned('pass1')">
<label for="pass2">Confirmez le</label>
<input class="keyboardInput" id="pass2" name="pass2" type='password' required onkeypress="checkCapsLock(event);" placeholder="" onblur="checkPassword('pass1','pass2'); Banned('pass1')" title="">
Vous avez activé la touche majiscule

the first code works, if i set two different passwords, it will alert me, if i modify it, the alert disappear. but in the second (Banned), it will not work (the user is not warned), and if i remove the Else part, it will work, but if it will detect a word that is in the list, it will never disappear even if the user modify the value with a string that dident belong to the list.

what is the problem?

Upvotes: 0

Views: 257

Answers (1)

a better oliver
a better oliver

Reputation: 26828

You do not exit the function when a condition is met, i.e. a password is on the ban list. What you want is something more like this:

for (var i = 0; i < ban_list.length; i++) {
  if (pas.value == ban_list[i]) {
    pas.setCustomValidity("Mot de Passe Facile, Veuillez le changer");
    return;
  }
}
pas.setCustomValidity("");

That way you will always get your alert if a password is on the ban list and the alert will disappear if it's not. Mind the position of pas.setCustomValidity(""); - it's outside the loop.

Upvotes: 1

Related Questions