From a user standpoint, these types of validations are annoying, and it seems like every site out there uses them this way. Miss one, get an alert. Miss another, get an alert. By the third or fourth one, I'm feeling pretty stupid, but the truth is I'm in a hurry and don't take time to read. :-) Many users are like this, which is why I like to put
all my alerts at once (not that I'll read those either, but it's worth a try.)
Plus this approach gets *extremely* handy on large forms, it cuts your code down by a few hundred lines.
You should **really** be using id's on your form, there are various reasons and making validation easier is one of them. You don't even have to pass a reference to the form if you do.
Last, note how I attached the action to the form in window.onload - say "no" to inline Javascript. :-)
<script type="text/javascript">
window.onload=function() {
document.getElementById('myform').onsubmit = function() { return Form1_Validator(); };
};
//
function Form1_Validator() {
var msg=''; // starts off empty. If it's not empty, there are errors.
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
var first_focus=''; // If multiple errors, focus only on the first.
// Create an association of form field to error message. Try to avoid
// id's/names that may conflict with the DOM or JS methods, like name, submit,
// search . . .
required = {
'your-name':'Please enter your NAME.',
'phone':'Please enter your phone number.',
'email':'Please enter a valid email address'
}
for (key in required) {
if (document.getElementById(key).value=='') {
msg += required[key] + "\n";
if (first_focus=='') { first_focus=key; }
}
}
// A "special" validation for email - a blank email and
// a poor match are two different conditions. Note you might want
// to do the same with the phone number.
if ((msg=='') &&
(! emailRegEx.test(document.getElementById('email').value)==true)) {
msg = "Please enter a valid email address.\n";
first_focus='email';
}
if (msg != '') {
alert(msg);
document.getElementById(first_focus).focus();
return false;
}
// No need for else, it won't get here if there's errors
return true;
}
</script>
(A note, I just tested both of these in a test file, this is working code.) Some changes to the form for the ID's which reveals another tool requiring ID's - form labels, for accessibility. Use semantic elements (<br> is rarely needed), and you're mixing HTML with XHTML:
0 value=""><br /> <!-- XHTML: <input/><br/> HTML: <input><br> -->
Pick one and roll with it.
<form name="theForm"
id="myform" action="coffeeorder.php" method="POST">
<p><span class="required">*</span><label for="your-name">Name:</label><input type="text" name="
your-name" id="your-name" size="30" value=""></p>
<p><span class="required">*</span><label for="phone">Phone:</label><input type="text" name="phone"
id="phone" size="6" value=""></p>
<p><span class="required">*</span><label for="email">Email:</label><input type="text" name="email"
id="email" size=30 value=""></p>
<p><input type="submit" name="
Submit-button" id="submit-button" value="Submit"><input type="reset"></p>
</form>