The below prevents the form to be submitted in Chrome, FF and Safari - but not in IE nor Opera:
The JavaScript:
function validateAll() {
var fFirstname = document.getElementById('firstname');
if (fFirstname.value == "") {
invalidFirstname.style.display = "block";
firstname.className = "Standard-Req";
firstname.focus();
alert('Please fill out First Name.');
return false;
}
The HTML:
<form name="Contact" action="somefile.php" method="post" onSubmit="return validateAll();">
<input type="text" name="firstname" class="Standard" id="firstname">
<input type="submit" name="Submit" value="Send Message" class="btn-Standard">
</form>
Now, if I get rid of the 3 commands before ALERT, it works in IE and Opera... Like this:
function validateAll() {
var fFirstname = document.getElementById('firstname');
if (fFirstname.value == "") {
alert('Please fill out First Name.');
return false;
}
I'd really like to be able to change the class, show an error icon (invalidFirstname) and focus the field if no data has been entered... Is this possible??
invalidFirstname
is not declared inside your function. Is it declared outside? If not, it can be causing an error. – bfavaretto May 17 '12 at 1:51