View Full Version : Trouble making error message

06-23-2008, 11:01 PM
Trouble with a script. I want an error message in red to print to the top of the form if the user didn't fill out two of the fields but it's not working - it doesn't stop like its supposed to, it still submits the form. Here's the part of my code I know has problems:

function CheckTheForm() {
var result = true;
var msg="";

if (document.frmTest.name.value=="" OR document.frmTest.eml.value=="") {
newHTML = "<span style=\"color:red\"> Please fill out all required fields.</span>";
document.getElementById('errortxt').innerHTML =newHTML;
result = false;

return result;

And then where I want it to print out:

<form name="frmTest" method="post" action="test_form.html">
<p id="errortxt"></p>
<table width="21%" border="0">
<td id="title">Name</td>
<td><input type="text" name="name" /></td>
<td id="eml">email</td>
<td><input type="text" name="eml" /></td>
<td><input type="submit" name="Submit" value="Submit" onclick="return CheckTheForm();" /></td>
<td><input type="reset" name="Reset" value="Reset" /></td>

Any ideas?

Arty Effem
06-24-2008, 02:42 AM
1) There is no OR operator in JS. The logical OR operator is ||.

2) Don't name a form element name or any other word that matches any form property.

3) Always check the error console.

06-25-2008, 05:28 PM
Thanks Arty Effem! that worked splendidly.