I have some jQuery that checks if the user inputted a correct email address. The user can then go back and change his/her email if it is not valid. However, my code will result in the same error messege twice because the error keeps getting appended inside my errors
div. How can I change this to only allow the error to be appended once? I also would like to remove the error if the email
variable is true
. There are also other validation errors being placed in this div.
jQuery:
var email = $('#email').val();
email = validateEmail(email);
if (email = "false") {
$('#errors').append("<p>Please enter a valid email</p>");
}
validateEmail
will return either true
or false
depending on whether the email is valid.
var email = $('#email').val();
email = validateEmail(email);
if (email == "false"){
$('#email_error').remove();
$('#errors').append("<p id='email_error'>Please enter a valid email</p>");
}
when u append the email error, specity it with a class "emailerror",
$('#errors').append("<p class="emailerror">Please enter a valid email</p>");
but before append, just remove that error which have class "emailerror"
var email = $('#email').val();
email = validateEmail(email);
if (email = "false"){
$('#errors .emailerror').remove();
$('#errors').append("<p class="emailerror">Please enter a valid email</p>");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With