Arkhall Arkhall - 5 months ago 45
Javascript Question

JQuery - Check if input is empty

I'm working on a password input.

I already check some answers on stackoverflow but I don't know applicate those answers to my code apparently.

Here is the code (take attention about the second condition if) :

$('#id_password.form-control').keyup(function(){
$('span.error-keyup-password').remove();
var inputVal = $(this).val();
var passwordReg = /^(?=.*\d)(?=.*[a-zA-Z])(?=.*[\W_]).{3,}$/;
if(!passwordReg.test(inputVal)){
$(this).after('<span style="color:red;" class="error error-keyup-password">Password must content at least 1 number, 1 letter and 1 special character.</span>');
}else if (inputVal.value.length == 0 ){
$('span.error-keyup-password').remove();
}
});
});


Normally my span will be removed if the length of my value is 0. But I don't know why I can't remove it with that condition. Okay my REGEX is not respected when the length is 0, but I don't want to display that message when I remove my password in the field.

Do you have an idea ? Thank you for taking time about my question.

Have a nice day :)

Answer

just use

  var inputlength= $(this).val().length;

then

else if (inputlength == 0 ){
  $('span.error-keyup-password').remove();
}
Comments