Check box validaton

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alexio
    New Member
    • Jan 2008
    • 17

    Check box validaton

    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>Untitl ed Document</title>
    <script language="javas cript">
    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="retur n validate();" >
    <body>
    <table width="905" border="0" cellpadding="1" cellspacing="1" class="h1">
    <tr>
    <td width="145" class="text" height="28"><di v align="right">
    <div align="right">B ank Name:&nbsp;</div>
    </div></td>
    <td width="314" height="28"><in put class="text" name="OBKey__20 6_1" size="50" tabindex="6"></td>
    <td width="214" class="text" height="28"><di v align="left">&n bsp;&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"><i nput name="OBKey_Gen eric_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"><in put name="OBKey_Gen eric_Field_02_1 " value="ON"type= "checkbox" tabindex="11">
    &nbsp;Cancel ed Check Request </td>
    </tr>
    <tr> </tr>
    <tr>
    <td width="250" height="26" class="text"><d iv align="left" class="h2A">
    <td height="26" class="text" colspan="2" valign=""><inpu t name="OBKey_Gen eric_Field_03_1 " value="ON" type="checkbox" tabindex="12">
    &nbsp;Stop Payment Request</td>
    </tr>
    <tr>
    <td width="250" height="35" class="text"><d iv align="left" class="h2A">
    <td height="35" class="text" colspan="2" valign=""><inpu t name="OBKey_Gen eric_Field_05_1 " value="ON" type="checkbox" tabindex="13">
    &nbsp;Other</td>
    </tr>
    <tr>
    <td width="250" height="32" class="text" valign="baselin e"><div align="left" class="h2A">
    <td align="left" valign="top" ><input type="submit" value=Submit name="OBBtn_Sav e" tabindex="14" >
    <input type="submit" value="Cancel" name="OBBtn_No" >
    </td>
    </tr>
    </table>




    </body>
    </html>[/HTML]
    Last edited by acoder; Jun 17 '08, 09:40 PM. Reason: Added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    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.getEle mentsByName().

    Comment

    Working...