Gossamer Forum
Home : General : Internet Technologies :

Javascript validation - check if at least one field has value

Quote Reply
Javascript validation - check if at least one field has value
Hi All

I'm trying to find a javascript that can check if at least one textbox field of a group of fields contains data

In other words, if all textbox fields in a specified group of fields are empty, then you get an alert. But, if at least one of that specified group of textbox fieldnames has content, then the form allows submit

if any (name,email,addr1,addr2,phone) contains content, then okay to submit - but if all (name,email,addr1,addr2,phone) are empty, then alert "hi, there. ya gotta fill in something"

I've been searching for quite a bit, but no luck so far. Tons of great validation code out there, some that work for groups of checkboxes, and lots of "if empty, alert" code available for a single text field, but I've never found one that could scan a group of field names in the way that I mentioned.

Code:
if (form.name.value == "" OR form.email.value == "" OR form.addr1.value == "" OR form.addr2.value == "" OR form.phone.value == "") {
alert( "Please enter some data." );
return false ;

Many thanks Smile

------------------------------------------

Last edited by:

Andy: Mar 10, 2005, 7:15 AM
Quote Reply
Re: [DogTags] Javascript validation - check if at least one field has value In reply to
Hi,

I think that should almost work. I use this code on one of my sites;

Code:
if (main.CWICat.value == "" && main.ADBCat.value == "" && main.ACUCat.value == "" && main.SCECat.value == "" && main.PSICat.value == "" && main.DeleteReason.value == "") {
alert( "Please select at least one category!" );
return false;
}

In theory, just changing && to OR should do the trick :)

Hope that helps.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Javascript validation - check if at least one field has value In reply to
Yep, thanks Smile

Code:
<script language="JavaScript">
<!--
function andyvalidate() {
if (form.Cat1.value == "" && form.Cat2.value == "" && form.Cat3.value == "" && form.Cat4.value == "" && form.Cat5.value == "" && form.Cat6.value == "") {
alert( "Please fill in at least one box" );
return false;
}
}
//-->
</script>

------------------------------------------

Last edited by:

DogTags: Mar 10, 2005, 7:55 AM