...

View Full Version : Validate At Least One Field is Filled



Kylena
03-08-2005, 04:30 AM
Hi,

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;
else
return true;
}

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

Kor
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++){
if(inp.getAttribute('type')=='text'){
if(inp[i].value.length>0){
q++
}
}
}
if(q==0){
alert('Please fill at least one field!');
return false;
}
}
...
...
<form onsubmit="return validateAdvSearch(this)">

RadarBob
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;

nextEntry:
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);'>

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

I'll test these 2 out :)

Kylena
03-09-2005, 08:43 AM
Hi,

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

if(inp.getAttribute('type')=='text'){

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:

theList.push(myForm.field1.value);

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.

RadarBob
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.

rwedge
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'); }
}
</script>
/Bob

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

if(inp[i].getAttribute('type')=='text'){



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum