Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple form field validation

Status
Not open for further replies.

whargra

MIS
Dec 5, 2000
7
US
I know how to validate each field for vaild input but how would I do this for something like a search page where I wanted at least one field filled in and an error to pop up if at least one field was not entered? The user can search on one field or multiple fields, just as long as at least one field was entered.

if (document.formName.fname.value == ''){
alert('First Name can't be blank!');
return false;
}
if (document.formName.lname.value == ''){
alert('Last Name can't be blank!');
return false;
}
if (document.formName.address.value == ''){
alert('Address can't be blank!');
return false;
}
if (document.formName.city.value == ''){
alert('City can't be blank!');
return false;
}
if (document.formName.state.value == ''){
alert('State can't be blank!');
return false;
}
return true;


Thanks.
 
are they all gonna be text fields?
if yes, here's an example for you:
Code:
var count = 0;
for (i=o; i< form_name.elements.length; i++) {
 if (form_name.elements[i].value != &quot;&quot;) {
 count = count + 1
 }
}
if (count == 0) {
alert(&quot;please fill in at least one field&quot;)
return false
}
else {
return true
}
--------------------------------------------------
Goals are dreams with deadlines
 
valeriav,

No, there is one Select drop down that I want to include and one Select drop down that I don't want to include(I'm leaving this one out completely because the return results would be too large if they only selected this field).

Thanks.
 
then, i don't knwo how to do it in one step, but you can try something like this:
Code:
var count = 0;
for (i=o; i< form_name.elements.length; i++) {
 if (form_name.elements[i].value != &quot;&quot;) {
 count = count + 1
 }
}
if (count != 0) { //if at least 1 text field was filled in
return ftrue
}
else { //if none of teh text fields are filled in
for (i=o; i<form_name.select_name.options.length; i++) {
if (form_name.select_name.optinos[i].selected == true) {
count = count + 1
}
}
if (count  == 0) {
alert(&quot;you did not enter at least one field&quot;)
return false
}
else {
return true
}
}
--------------------------------------------------
Goals are dreams with deadlines
 
ooops, actually here is the code with some corrections:
Code:
var count = 0;
for (i=o; i< form_name.elements.length; i++) {
 if (form_name.elements[i].type == &quot;text&quot; && form_name.elements[i].value != &quot;&quot;) {
  count = count + 1
 }
}

if (count != 0) { //if at least 1 text field was filled in
return true
}
else { //if none of teh text fields are filled in
 for (i=0; i<form_name.select_name.options.length; i++) {
  if (form_name.select_name.optinos[i].selected == true) {
   count = count + 1
  }
 }
if (count  == 0) {
alert(&quot;you did not enter at least one field&quot;)
return false
}
else {
return true
}
}
--------------------------------------------------
Goals are dreams with deadlines
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top