View Full Version : Validate At Least One Field is Filled

03-08-2005, 04:30 AM

I'm trying to visualise how should I validate a form that only requires that only one of the fields is filled.

I've the validation code to validate mandatory fields. But how should I code this if there are maybe 5 fields (different types) and I want the user to enter the search keywords in any of the fields?

I believe that's how the advanced search in this forum is done. :D

Here's what I have:

function validateAdvSearch()
var f = document.forms[0];

if (validateText(f.Name, 'Name') == false || validateCombobox(f.Country, 'Country') == false)
return false;

else if (validateDate(f.Date, 'Date') == false)
return false;
return true;

Hmm... it doesn't seem the correct way to code it, anyone can point me in the right direction?

03-08-2005, 11:56 AM
I'm trying to visualise how should I validate a form that only requires that only one of the fields is filled.

Circle through elements

function validateAdvSearch(f){
var q=0;
var inp = document.getElementsByTagName('form')[0].getElementsByTagName('input');
for(var i=0;i<inp.lenght;i++){
alert('Please fill at least one field!');
return false;
<form onsubmit="return validateAdvSearch(this)">

03-09-2005, 01:26 AM
Kor's solution is pretty nifty.

I often have the problem that at least one of a specific subset of fields must be entered. I create an array of those values and then search the array.

This technique is also pretty flexible. If your list is different, and as fields are added or removed over time, only the "buildAtLeast1Array()" needs to be touched.

Also, by passing in the form object as a parameter to the function, rather than reference the form directly in the function, the function(s) are not "coupled" to a specific form / web page - thus the code is more "cut and pasteable", i.e. reusable.

function buildAtLeast1Array(myForm) {
var theList = new Array();
theList.push(myForm.field1.value); // repeat this line as necessary,
// Taylored to type of input tag

return theList;

// by passing in the entryList, this function is reusable for
// any subset (array) of entry fields.
function anythingEnteredIn(entryList) {
var isEntered = true;

for (i=0; i<entryList.length; i++) {
if (entryList[i] == "") {
isEntered = false;
break nextEntry; // stop looping now

return isEntered;

function validate(myForm) {
var isValid = true;
var myEntryFields = buildAtLeast1Array(myForm);

if (!anythingEnteredIn (myEntryFields)) {
isValid = false;
alert("you must make at least one entry");

// enter other form validations, setting 'isValid' to false as necessary

return isValid;

-- meanwhile, back on your form tag...
<form name='myForm' method='post' onSubmit='return validate(this);'>

03-09-2005, 04:35 AM
Thank you Kor, Bob!

I'll test these 2 out :)

03-09-2005, 08:43 AM

I tested both functions but I can't get them to work.
I used Kor's function, there's an error in this line:


I'm assuming that it can continue looping if the the form contains only textboxes? So, I should add in the following:

else if(inp.getAttribute('type') == 'select') { blah blah.. }

As for Bob's the error points that field1.value is null or blank:


I tried to change it to my field name but the same error occur. I can't make head or tails of what it means.

03-14-2005, 10:11 PM
Well, hard to say what's up.

Make sure that in "myForm.field1.value" that 'field1' is the the name attribute; spelled correctly. Capitalization counts.

Next ... "is null or blank". I'd say blank is an expected condition - sometimes we don't enter anything into a field. If it's null, that could indicate that you do not "have a hold" of the field - like maybe it's spelled wrong.

Next try alert() inside buildatLeast1Array() to see what the values are.

03-15-2005, 12:18 AM
Here's some more code to try.

<script type="text/javascript">
function validateAdvSearch() {
var f = document.forms[0];
var num = f.elements.length;
var j = 0;
for (var i=0; i<num; i++) {
if (f.elements[i].type == 'checkbox') { if (f.elements[i].checked) {j = j+1; } }
if (f.elements[i].type == 'text') { if (f.elements[i].value) { j = j+1; } }
else { continue; }
if (j == 0) { alert('At Least One Field Must Contain a value'); }

03-15-2005, 08:29 AM
my fault, should've been