Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Aug 2008
    Location
    Chester, United Kingdom
    Posts
    28
    Thanks
    2
    Thanked 1 Time in 1 Post

    Validating a radio button

    Below linked is the last page of a 13 page form.

    It carries across HIDDEN VALUES and then sends them to an ASP processor which sends to an email,

    All I need is the script to validate the radio button, making it essential to be checked before the form and the values are submitted.

    Simple fix anyone?

    Cheers
    Chris SS

    http://cmschester.com/securedloanform/form13.asp

    Here's my javascript at the moment
    Code:
    <script type="text/javascript">
     <!--//
      function agree(){
        if(!document.forms[0].elements[4].checked){
           alert('You must confirm that you are able to disclose information about this applicant to submit this application, please click the I CONFIRM button');
           return false;
         }
         else { return true };
       }
     //-->
    </script>
    and the code for the radio button.

    Code:
    <input name="Submit" type="submit" class="FormBUTTON" value="Apply now" onsubmit="return agree()">
    Help would be appreciated!
    UK Marketing Agency, developing brand identities to company websites - Please view www.suttonsilver.co.uk

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,521
    Thanks
    3
    Thanked 507 Times in 494 Posts
    Code:
    <form action="processor2.asp" method="post" name="form13" target="_self" id="form13" onsubmit="return agree()">
    Code:
    <input name="Submit" type="submit" class="FormBUTTON" value="Apply now">
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Also, this part does'nt target the agreement radio button:
    Code:
    document.forms[0].elements[4].checked
    It's the first element in the form, so it should be:
    Code:
    document.forms[0].elements[0].checked)
    ...but that is prone to error since we have to change the index when we add new element especially above the radio button.

    The best way for you to do, is to give your radio button a unique identifier:
    Code:
    <input type="radio" name="x13_Agreement" value="Yes" id="agree">
    and use this script instead:
    Code:
      function agree(){
        if(!document.getElementById('agree').checked){
           alert('You must confirm that you are able to disclose information about this applicant to submit this application, please click the I CONFIRM button');
           return false;
         }
         else { return true };
       }
    Hope it helps.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #4
    New Coder
    Join Date
    Aug 2008
    Location
    Chester, United Kingdom
    Posts
    28
    Thanks
    2
    Thanked 1 Time in 1 Post
    Hi Both,

    Thank you very much for your response!

    I have implemented your code rangana

    But I still can't seem to get this to work?!

    Could you please check the page again on the same link and tell me If I have missed something?
    UK Marketing Agency, developing brand identities to company websites - Please view www.suttonsilver.co.uk

  • #5
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,521
    Thanks
    3
    Thanked 507 Times in 494 Posts
    Code:
    
       <input type="radio" name="x13_Agreement" value="Yes" id="agree">
    Code:
    <form action="processor2.asp" method="post" name="form13" target="_self" id="form13" onsubmit="return Agree()">
    Code:
    <input name="Submit" type="submit" class="FormBUTTON" value="Apply now" >
    Code:
      function Agree(){
        if(!document.getElementById('agree').checked){
           alert('You must confirm that you are able to disclose information about this applicant to submit this application, please click the I CONFIRM button');
           return false;
         }
         else { return true };
       }
    function/ids/variable names must be unique.
    also suggest using a checkbox as opposed to a radio.
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #6
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hello, I tried to implement this into my script but it is not working.. I have a long form that needs multiple fields validated in addition now I need to validate some radio buttons. The script I am running now is working but I can not figure out how to get it to validate a radio button group.

    Here is my current validation script
    Code:
    <script type="text/javascript">
    <!--
    function UpdateForm (obj1) {
    
    var themessage = "You are required to complete the following fields:\n ";
    
    if (obj1.first.value== ""  ) {
    themessage = themessage + " - First Name\n";
    }
    
    if (obj1.last.value== ""  ) {
    themessage = themessage + " - Last Name\n";
    }
    
    if (obj1.nick.value== "" ) {
    themessage = themessage + " - Nick\n";
    }
    
    if (obj1.age.value== "" ) {
    themessage = themessage + " - Age\n";
    }
    
    if (obj1.seat.value== "" ) {
    themessage = themessage + " - Seat\n";
    }
    
    if (obj1.email.value== "" ) {
    themessage = themessage + " - E-Mail\n";
    }
    
    if (obj1.address1.value== "" ) {
    themessage = themessage + " - Address1\n";
    }
    
    if (obj1.city.value== "" ) {
    themessage = themessage + " - City\n";
    }
    
    if (obj1.state.value== "" ) {
    themessage = themessage + " - State\n";
    }
    
    if (obj1.zip.value== "" ) {
    themessage = themessage + " - Zip\n";
    }
    
    if (obj1.emergencyname.value== "" ) {
    themessage = themessage + " - Emergency Contact Name\n";
    }
    
    if (obj1.emergencyphone.value== "" ) {
    themessage = themessage + " - Emergency Contact Phone\n";
    }
    
    if (obj1.payat.checked== "" ) {
    themessage = themessage + " - Please choose a method of payment\n";
    }
    
    //alert if fields are empty and cancel form submit
    
    if (themessage == "You are required to complete the following fields:\n " ) {
    obj1.submit();
    }
    else {
    alert(themessage);
    return false;
       }
    
       obj1.os0.value = obj1.nick.value;
       obj1.os1.value = obj1.seat.value;
    }
    -->
    </script>
    Here is the button array
    Code:
      <tr>
        <td><div align="right">PayPal: </div></td>
        <td><input name="payat" type="radio" value="PayPal" /></td>
      </tr>
      <tr>
        <td><div align="right">Action City: </div></td>
        <td><input name="payat" type="radio" value="Action City" /></td>
      </tr>
      <tr>
        <td><div align="right">Chaos Computers: </div></td>
        <td><input name="payat" type="radio" value="Chaos Computers" /></td>
      </tr>
      <tr>
        <td><div align="right">Computer Wizards: </div></td>
        <td><input name="payat" type="radio" value="Computer Wizards" /></td>
      </tr>
      <tr>
        <td><div align="right">Friend paid for my seat: </div></td>
        <td><input name="payat" type="checkbox" value="Paid by Friend" onclick="toggle(this);alert( 'We DO verify your payment status information.'); " /></td>
      </tr>
    I know the last one is a checkbox but it should still validate to true if checked I'm assuming...

    Other than not being able to validate the "payat" button array it works fine. Can you maybe shed some insight on this for me? Thanks

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,147
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Twizted Indian - to avoid confusion it would be best if you started your own thread.

    All the radio buttons must have the same name, but your checkbox may not have the same name as the radio buttons.

    To check which radio (if any) was selected:-

    Code:
    <script type = "text/javascript">
    
    function chk() {
    for (var i = 0; i < document.myFormName.payat.length; i++) {
    if (document.myform.payat[i].checked) {
    var sel = i;
    }
    }
    if (sel) {
    alert ("Radio " + sel + " selected")
    }
    else {
    alert ("No radio button was selected");
    }
    }
    
    </script>
    You may wish to alter the checkbox to another radio, and then:-

    if (sel == 4) {
    var verify = confirm ( 'We DO verify your payment status information.');
    if (verify) {
    // proceed
    }
    else {
    // cancel booking
    }
    }
    Last edited by Philip M; 12-16-2008 at 08:52 AM. Reason: Typo


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •