The validation works, and the alert pops up when the email address is not valid, but then it goes and submits it after you click ‘Ok’ anyway. How do I stop it from processing the action?
Code sample:
<input type="submit" onclick="validate()" />
function validate() {
var email = $('input[name=email]').val();
if (!/(.+)@(.+){2,}\.(.+){2,}/.test(email)) {
alert('Please enter a valid email address');
return false;
}
}
Thanks!
then
return trueif validation is successful andfalseif not…you can also attach the validation to the form itself:
or with jquery: