0 Replies - 168 Views - Last Post: 31 October 2009 - 09:31 PM

#1 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

At least one text box must be fill

Posted 31 October 2009 - 09:31 PM

Description: sometime you have many textboxes and you want at least one textbox must not be empty other wise the form will not submit.
<script type="text/javascript">
function check_textboxes(frmName){
	var frm = document.getElementById(frmName).elements;
	var count = frm.length;
	var i;
	var empty = false;
	for(i=0;i<count;i++)
	{
		if(frm[i].type=="text"){
			if(frm[i].value != ""){
				empty = true;
			}
		}
	}
	
	if(empty==false){
		alert("Please fill at least one text box");
	}
	return empty;	
}
</script>

<form name="form1" id="form1" method="post" action="" onsubmit="return check_textboxes(this.id)">
  <table width="400" border="0" cellspacing="1" cellpadding="2">
    <tr>
      <td width="122">Text Box1</td>
      <td width="278"><label>
        <input type="text" name="txt1" id="txt1">
      </label></td>
    </tr>
    <tr>
      <td>Text Box2</td>
      <td><input type="text" name="txt2" id="txt2"></td>
    </tr>
    <tr>
      <td>Text Box3</td>
      <td><input type="text" name="txt3" id="txt3"></td>
    </tr>
    <tr>
      <td>Text Box4</td>
      <td><input type="text" name="txt4" id="txt4"></td>
    </tr>
    <tr>
      <td>Text Box5</td>
      <td><input type="text" name="txt5" id="txt6"></td>
    </tr>
    <tr>
      <td>Text Box6</td>
      <td><input type="text" name="txt6" id="txt5"></td>
    </tr>
    <tr>
      <td> </td>
      <td><label>
        <input type="submit" name="button" id="button" value="Submit">
      </label></td>
    </tr>
  </table>
</form>


Is This A Good Question/Topic? 0
  • +

Page 1 of 1