I have the following code which checks email addresses are valid. I want to remove the “Nice!..” part if the email address is ok and just have the form submit. How can I do this?
$(document).ready(function(e) {
$('#email-submit').click(function() {
var sEmail = $('#email').val();
// checking empty fields
if (validateEmail(sEmail)) {
alert('Nice!! your Email is valid, now you can continue..');
}
else {
alert('Invalid Email Address');
e.preventDefault();
}
});
});
Also, I have noticed if the email address is invalid, the alert works, but then goes to a page with the form on - like an external page. The form action is linking to an external URL as the action is hosted externally. Is there a way I can stop the form submitting when the email is invalid?
Thanks, this worked
However, when it prompts that an invalid email is entered, the page then loads a new page with the form on again - on an external URL. Is this because the form action is on an external URL? If so, is it possible to stop it from going any further until a valid email is added?
No, it’s most likely because the event handler is calling the function without “return” pre-pended to it. If you use something like (pseudo-code) onSubmit = "return functionName();" then the return false should prevent the form submit.
HTH,
UPDATE: Okay… looking closer at your code, you’re not using onSubmit or something like it. I’m assuming that “email-submit” is a submit button? Try changing that to an input type="button" so the submit button won’t submit, and use formname.submit() (whatever the form name/id is) to submit.
It would go AFTER the if conditional validating email. And you would replace formname with the actual name/id of your form.
Also, replace your submit button with an input type="button" so the form doesn’t submit if it doesn’t pass validation (keep the button name, of course).