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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Required Field 1

Status
Not open for further replies.

LOW

Programmer
Sep 27, 2000
45
US
I have a form w/5 fields used for a search. It is required that one of the fields (doesn't matter which one) has input. If none of the fields has input, I either want an alert box to pop up or to block the submit. I know how to do this w/specific required fields but not sure how to accomplish this when it doesn't matter which field is filled out. I'd appreciate any ideas.
 
Hi

The simplest :
Code:
<html>
<head>
<title></title>
<script type="text/javascript">
[b]function[/b] check()
{
  [b]var[/b] df=document.fo;
  [b]if[/b] (df.one.value+df.two.value+df.three.value+df.four.value+df.five.value=='') {
    alert([i]'Fill at least one field'[/i]);
    [b]return false[/b];
  }
  [b]return true[/b];
}
</script>
</head>
<body>
<form action="" name="fo" onsubmit="return check()">
<input type="text" name="one">
<input type="text" name="two">
<input type="text" name="three">
<input type="text" name="four">
<input type="text" name="five">
<input type="submit">
</form>
</body>
</html>

Feherke.
 
Hi

One nicer & better way :
Code:
<html>
<head>
<title></title>
<script type="text/javascript">
[b]function[/b] check(form)
{
  [b]var[/b] elemLen=form.elements.length;
  [b]var[/b] ok=[b]false[/b];
  [b]for[/b] (var i=0;i<elemLen;i++) [b]if[/b] (form.elements[i].type==[i]'text'[/i] && form.elements[i].value.match(/\S/)) ok=[b]true[/b];
  [b]if[/b] (!ok) alert([i]'Fill at least one field'[/i]);
  [b]return[/b] ok;
}
</script>
</head>
<body>
<form action="" name="fo" onsubmit="return check(this)">
<input type="text" name="one">
<input type="text" name="two">
<input type="text" name="three">
<input type="text" name="four">
<input type="text" name="five">
<input type="submit">
</form>
</body>
</html>

Feherke.
 
Thanks so much for you help, Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top