I have been writing a code for validating forms using javascript/jquery. Following is a code snippet for checking email format. The problem is when I enter invalid email, it recognizes it, but when I go back to this field and enter correct email, the error text still stays even though I have used the ‘else’ part. How do I remove the error text in this case?
if(e1.value!=''){
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
if (document.signupForm.email1.value.search(emailRegEx) == -1) {
$("#err_email1").html("Please enter valid email address.");
}
status=0;
}
else{
$("#err_email1").html("");
status=1;
}
Your else block to run the code which removes the error message is only running when the email field is blank, which is presumably not what you want, try the edit below.