I need to solve a small problem. First of all, the code works, the question is that in the password fields , if I modify the password after entering the password confirmation, it creates a bug and does not update the password confirmation error message. I think it may be because I am using the function keyup
, which one should I use or would you recommend me to improve the code?
From already thank you very much!
function validInput(input){
var type = $(input).attr('type');
var id = $(input).attr('id');
var error=false;
var msg= "";
var str_e = /^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/;
if(id === "name"){
if (input.val().length === 0) {
error=true;
msg= "Porfavor ingresa un nombre";
}else if(input.val().length > 30){
error=true;
msg= "El nombre es muy largo";
}
}else if(id === "lastname"){
if (input.val().length === 0) {
error=true;
msg= "Porfavor ingresa un apellido";
}
}else if(id === "pass"){
if (input.val().length === 0){
error=true;
msg= "Porfavor ingresa una contraseña";
}
}else if(id === "pass2"){
if (input.val() !== $('#pass').val()){
error=true;
msg= "Las contraseña no coinciden";
}
}else if(id === "email"){
if (input.val() === 0 || !str_e.test(input.val())) {
error = true;
msg = "Corre electrónico invalido.";
}
}
if (error=== true) {
input.removeClass().addClass('error-input');
input.next().removeClass().addClass("error");
var next = input.next('.error');
$(next).children(".msg").text(msg);
}else{
input.removeClass().addClass('succes-input');
input.next().removeClass().addClass("succes");
var next = input.next('.succes');
$(next).children(".msg").text();
}
return input;
}
$('input').keyup(function(){
validInput($(this));
});
It does not update the message since you do not verify that it is the same or different with
pass2
, as you do when theid === "pass2"
, there you validate that it is different withYou must do the same when the id is
pass
, also take into account that when the id ispass2
,no se valida que no esté vacío el campo
Recommendation :
Qué usar?
, One option would be to use the focusoutFocus
Event to validate each time the of is lostInput
, although the event used is valid if you want to do the validation in Real timeYou are only updating in case they are not the same, but you do not place the other cases, as it is when it is empty.
Is worth to say: