I have a very basic form validation script, that is very old, and stopped working in Firefox around the time it got to v3.0.
I’ve already done some googling trying to find a replacement script, but the most recent articles I can find are from around 2005. And most are older than that.
It doesn’t have to be super fancy – if all it does is make sure the required fields aren’t empty, that would be enough – but neither would I object to a more complete script which checks the email and/or phone fields for validly formatted input.
You can see the form in action here.
And the code:
<h2>Via Email (<span class="required">»</span> indicates required field)</h2>
<form name="contact" id="contact" action="thankyou.php" method="post" onSubmit="return validateform()">
<script language="JavaScript">
// browser check
ns4 = (document.layers)? true:false
ie4 = (document.all)? true:false
function layerWrite(id,text) {
if (ns4) {
// var lyr = eval("document." + id + ".document");
var lyr = document.desc.document;
lyr.open()
lyr.write(text)
lyr.close()
}
else if (ie4) document.all[id].innerHTML = text
}
function validateform() {
var errormsg = "";
if (document.contact.name.value == "") {
errormsg = "Please complete all required fields.";
}
if (document.contact.emailaddr.value == "") {
errormsg = "Please complete all required fields.";
}
if (document.contact.phone.value == "") {
errormsg = "Please complete all required fields.";
}
if (document.contact.service.value == "") {
errormsg = "Please complete all required fields.";
}
if (document.contact.messagetext.value == "") {
errormsg = "Please complete all required fields.";
}
if (errormsg == "") {
return true;
} else {
layerWrite( "desc", errormsg );
return false;
}
}
</script>
<div id="desc"> </div>
<p><span class="required">»</span> Name: <input name="name" type="text" style="position:relative; left:25px;"><br>
<span class="required">»</span> Email: <input name="emailaddr" type="text" style="position:relative; left:28px;"><br>
<span class="required">»</span> Phone: <input name="phone" type="text" style="position:relative; left:21px;"></p>
<p><span class="required">»</span> Which service are you interested in?
<select name="service" style="position:relative; left:50px;">
<option value="" selected>Please select</option>
<option value="Gameheads">Gameheads</option>
<option value="Birds">Birds</option>
<option value="Lifesize">Lifesize</option>
<option value="Fish">Fish</option>
<option value="Showroom">Showroom</option>
<option value="Other">Other</option>
</select>
<br>
How did you hear about Sportsman's Image?
<input name="referredby" type="text" style="position:relative; left:13px;"></p>
<p><span class="required">»</span> Message:
<textarea name="messagetext" cols="30" rows="3" style="position:relative; left:10px; vertical-align:text-top;"></textarea></p>
<input type="submit" value="Submit" style="position:relative; left:185px;">
</form>
TIA.