When a form submit button is clicked, a function to validate all the field is to be called. Nothing is to happen, however, if the validation fails.
I am using mailto: as my action, does this make a difference?
I would like to get clarification on two things:
-
Is this the correct way to call a function when clicking the submit button?
$(document).ready(function(){ $('#contactForm').submit(function(){ checkMail(); }); }); -
Can I still validate the fields even though I’m using
mailto:?
Here is the rest of the code:
function checkEmail(){
var email = document.contact.email.value;
if(email == "") {
document.getElemtById("email_error").innerHTML = "No Email Address";
return false;
}
else {
document.getElementById("email_error").innerHTML = ""
return true;
}
}
HTML:
<form name="contact" action="mailto:exampleemail@hotmail.com" method="post">
<li>
<label for="email">Email</label>
<input id="email" type="text" name="email" placeholder="Enter Email Address">
</li>
<span id="email_error"></span>
Further, I don’t get an error message on clicking submit.
No, you need the event handler to
return falsein case the validation failed. This will prevent the action from being executed, i.e. the mail program from being launched.Source
Modify it like this:
Of course, this implies that the
validate()function needs to actually returnfalsein case the validation fails, andtrueotherwise.Further you are missing
id="contactForm"on your<form>tag.Also, you need to grab the email value correctly:
There’s another mistake: You misspelled
getElementById(). Here’s a corrected version:Or alternatively, using all jQuery: