Arkhall Arkhall - 1 year ago 236
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) :

var inputVal = $(this).val();
var passwordReg = /^(?=.*\d)(?=.*[a-zA-Z])(?=.*[\W_]).{3,}$/;
$(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 ){

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 Source

just use

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


else if (inputlength == 0 ){
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download