Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
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
Old 03-10-2005, 09:06 AM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
here is a trim function .. you could trim whitespace off the input and then validate.

Code:
 function trim(str){
   return str.replace(/^\s*|\s*$/g,"");
}
function atleastonenonemptyfield() {
 if (trim(contactform.name.value) == "" && trim(contactform.email.value) == "" && trim(contactform.addr1.value) == "" && trim(contactform.addr2.value) == "" && trim(contactform.phone.value) == "" && trim(contactform.zip.value) == "") { 
    alert( "Please fill in at least one box" ); 
    return false; 
  }
}
i haven't actually tested that, but it seems like it should work.
__________________
Mike
sde is offline   Reply With Quote
Old 03-10-2005, 09:29 AM   #3 (permalink)
DogTags
Registered User
 
Join Date: Mar 2005
Posts: 8
DogTags is on a distinguished road
Thanks sde

I plugged it in, and it seems to be working

If a field has one whitespace and then some real content, it doesn't strip the whitespace (as far as I can see - I'm gonna test some more later) - that's good in this case.

I ran into a prob.

If no field has any content, the script does give you the alert, but as soon as you click the alert, the form still tries to submit to ACTION="http://site/blah/"

In other words, the form is trying to submit the data whether or not it passes the validation test.

Would there be a way where if the alert is triggered, you click the OK and the page just sits there while the visitor enters real data, and then the visitor clicks submit again and assuming valid data has been entered, then the script submits the data?

Many thanks
DogTags is offline   Reply With Quote
Old 03-10-2005, 10:53 AM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
in your form tag, the onsubmit needs to evaluate to false for the form not to submit. i see you have the function returning false already, so u just need to add 'return' before the function call. example:
Code:
onsubmit="return atleastonenonemptyfield();"
__________________
Mike
sde is offline   Reply With Quote
Old 03-10-2005, 01:29 PM   #5 (permalink)
DogTags
Registered User
 
Join Date: Mar 2005
Posts: 8
DogTags is on a distinguished road
Ah, great. It works

Thanks so much, Stan. This is a big help
DogTags is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculated field in Access 2003 form codeboi MS Technologies ( ASP, VB, C#, .NET ) 1 03-03-2005 07:55 AM
How to test for empty date from MySql call? Wysocki PHP 3 02-06-2005 02:17 PM
SQL- Calculated Field fdtoo Everything SQL ( MySQL, MSSQL, DB2, Postgre, Oracle, etc...) 0 09-04-2004 01:37 AM
Multiple Entry Fields for One Database Field heartsoh HTML, XML, Javascript, AJAX 0 07-12-2004 09:54 PM
DataReader empty watherton MS Technologies ( ASP, VB, C#, .NET ) 3 06-15-2004 12:58 PM


All times are GMT -8. The time now is 03:19 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting