View Single Post
Old 03-10-2005, 08:25 AM   #1 (permalink)
DogTags
Registered User
 
Join Date: Mar 2005
Posts: 8
DogTags is on a distinguished road
At least one non-empty field - whitespace issue

Hi All

I have a script that I'm workin on. It validates a group of text fields and gives an alert if none of the fields contains data.

The test is that there must be at least one field with real content. Otherwise, you get the alert. The bugger is that the script thinks that whitespace by itself is allowable content, and that's the rub.

Thus, if you have a situation where all 6 fields contain nothing or only whitespace, then you get the alert. BUT, if any field contains any real content (other than just whitespace), then the form is allowed to pass.

Example: Pass
name: (nuthin)
email: (nuthin)
addr1: (whitespace)
addr2: (whitespace)
phone: "blah"
zip: (whitespace)

Example: Fail
name: (nuthin)
email: (nuthin)
addr1: (whitespace)
addr2: (whitespace)
phone: (whitespace)
zip: (whitespace)

I hope I have explained this well enough

Here's the form. What do you think?

Code:
<HTML>
<HEAD>
<TITLE>At Least One non-empty field</TITLE>
<script language="JavaScript">
<!--
function atleastonenonemptyfield() {
 if (contactform.name.value == "" && contactform.email.value == "" && contactform.addr1.value == "" && contactform.addr2.value == "" && contactform.phone.value == "" && contactform.zip.value == "") { 
    alert( "Please fill in at least one box" ); 
    return false; 
  }
  }
//-->
</script>


</HEAD>
<BODY>
<FORM NAME="contactform" ACTION="http://site/blah/" METHOD="post" onsubmit="atleastonenonemptyfield();">
<BR>
name<input type="text" name="name"><br>
email<input type="text" name="email"><br>
addr1<input type="text" name="addr1"><br>
addr2<input type="text" name="addr2"><br>
phone<input type="text" name="phone"><br>
zip<input type="text" name="zip"><br>
<INPUT TYPE="SUBMIT">
</FORM>
</BODY>
</HTML>
DogTags is offline   Reply With Quote