By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,255 Members | 1,701 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,255 IT Pros & Developers. It's quick & easy.

Check box validaton

P: 17
I am a newbie to this and am having a problem with validation and keeping data that has been entered in other fields when submitting the form.
For the check boxes, I need a minimum of one selected. If one is not selected, a message box appears notifying that a check box has not been selected. When I click OK, the data in the other text boxes is removed as if the form is being reloaded. How can I prevent this from happening.
Is there a better way to validate the checkboxes. I have searched and most instances of validation refer to linking to an outside validation jsp. This can not be done for this situation. Any help would be most appreciated.


[HTML]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script language="javascript">
function validate(){
var A = document.forms[0].elements['OBKey_Generic_Field_01_1'];
var B = document.forms[0].elements['OBKey_Generic_Field_02_1'];
var C = document.forms[0].elements['OBKey_Generic_Field_03_1'];
var D = document.forms[0].elements['OBKey_Generic_Field_05_1'];

if (A.checked == 1){
alert("Are you sure you want to submit form?");
return false;
}
if (B.checked == 1){
alert("Are you sure you want to submit form?");
return false;
}
if (C.checked == 1){
alert("Are you sure you want to submit form?");
return false;
}
if (D.checked == 1){
alert("Are you sure you want to submit form?");
return false;
}

else
alert("You must select type of additional documentation submitted")
}

</script>




</head>
<form action="" method="post" name="ACH" onSubmit="return validate();" >
<body>
<table width="905" border="0" cellpadding="1" cellspacing="1" class="h1">
<tr>
<td width="145" class="text" height="28"><div align="right">
<div align="right">Bank Name:&nbsp;</div>
</div></td>
<td width="314" height="28"><input class="text" name="OBKey__206_1" size="50" tabindex="6"></td>
<td width="214" class="text" height="28"><div align="left">&nbsp;&nbsp;&nbsp; ABA # (must be 9 digits:&nbsp; </div></td>
<td width="219" height="28" ><div align="left">
<input name="OBKey__60_1" type="text" size="9" maxlength="9" tabindex="7" >
</div></td>
</tr>

</table>
<tr>
<td ><hr></td>
</tr>
<table width="901" border="0" cellpadding="1" cellspacing="1" class="h1">
<tr>
<td width="250" height="22" class="h2A">VI. Additional documentation submitted:</td>
<td width="644" height="22" colspan="2" class="text"><input name="OBKey_Generic_Field_01_1" value="ON" type="checkbox" tabindex="10">
&nbsp;Direct Allocated (Screen Prints)</td>
</tr>
<tr>
<td width="250" height="26" class="text"></td>
<td height="26" class="text" colspan="2"><input name="OBKey_Generic_Field_02_1" value="ON"type="checkbox" tabindex="11">
&nbsp;Canceled Check Request </td>
</tr>
<tr> </tr>
<tr>
<td width="250" height="26" class="text"><div align="left" class="h2A">
<td height="26" class="text" colspan="2" valign=""><input name="OBKey_Generic_Field_03_1" value="ON" type="checkbox" tabindex="12">
&nbsp;Stop Payment Request</td>
</tr>
<tr>
<td width="250" height="35" class="text"><div align="left" class="h2A">
<td height="35" class="text" colspan="2" valign=""><input name="OBKey_Generic_Field_05_1" value="ON" type="checkbox" tabindex="13">
&nbsp;Other</td>
</tr>
<tr>
<td width="250" height="32" class="text" valign="baseline"><div align="left" class="h2A">
<td align="left" valign="top" ><input type="submit" value=Submit name="OBBtn_Save" tabindex="14" >
<input type="submit" value="Cancel" name="OBBtn_No" >
</td>
</tr>
</table>




</body>
</html>[/HTML]
Jun 17 '08 #1
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
Your validation is wrong way round. If one is checked, return true, not false. Also, the way to validate would be check if none of them are checked. If so, return false. At the end of the validation function, return true.

Since the checkboxes are connected, use the same name, so you can get them at once with document.getElementsByName().
Jun 17 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.