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 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2009
    Location
    Portland, Oregon
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How to validate a radio button in a form

    I have some code I use for form validation but can't figure out how to make it work for a radio button. The radio button:

    Code:
    <INPUT type="Radio" NAME="BestWayToContact" VALUE='Call Me'>phone
    <INPUT type="Radio" NAME="BestWayToContact" VALUE='By Email'>e-mail
    Validation code that works for text fields

    Code:
    if ((document.form.phone.value == "Phone Number") ||
    (document.form.phone.value == "")) {
    missinginfo += "\n - Phone Number";
    }
    My feeble attempt (I know I need some sort of logic here)

    Code:
    if ((document.form.BestWayToContact.value == "Call Me") ||
    (document.form.BestWayToContact.value == "By Email")) {
    missinginfo += "\n - Best Way To Contact You";
    }
    Thanks in advance for your time and help!

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    What are you trying to do? It seemed you are not getting the value of the form. Anyway, might help:
    Code:
    var radCon = document.form2.elements['BestWayToContact'];
    alert('Best Way to Contact is via: '+(radCon[0].checked?radCon[0].value:radCon[1].value));
    Learn how to javascript at 02geek

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

  • #3
    New Coder
    Join Date
    Jan 2009
    Location
    Portland, Oregon
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Sorry Rangana but I just gave you a snipet of the script. Here's the whole thing
    Code:
    <SCRIPT LANGUAGE="JavaScript"> 
    function checkFields() { 
    missinginfo = ""; 
    if ((document.form.realname.value == "Name") ||
    (document.form.realname.value == "")) { 
    missinginfo += "\n - Name"; 
    } 
    if ((document.form.phone.value == "Phone Number") ||
    (document.form.phone.value == "")) { 
    missinginfo += "\n - Phone Number"; 
    } 
    if ((document.form.email.value == "") || 
    (document.form.email.value.indexOf('@') == -1) || 
    (document.form.email.value.indexOf('.') == -1) ||
    (document.form.email.value == "E-mail")) { 
    missinginfo += "\n - E-mail address"; 
    } 
    if (document.form.Message.value == "") { 
    missinginfo += "\n - Comments";
    } 
    if (document.form.BestWayToContact.value == "") { 
    missinginfo += "\n - Best Way To Contact You"; 
    } 
    
    if (missinginfo != "") { 
    missinginfo ="_____________________________\n" + 
    "You failed to correctly fill in your:\n" + 
    missinginfo + "\n_____________________________" + 
    "\nPlease re-enter and submit again!"; 
    alert(missinginfo); 
    return false; 
    } 
    else return true; 
    } 
    </script>
    Thanks!

  • #4
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Change this part:
    Code:
    if (document.form.BestWayToContact.value == "") {
    ...to:
    Code:
    var radCon = document.form.elements['BestWayToContact'];
    if (!(radCon[0].checked||radCon[1].checked)) {
    Learn how to javascript at 02geek

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

  • Users who have thanked rangana for this post:

    drumz (01-22-2009)

  • #5
    New Coder
    Join Date
    Jan 2009
    Location
    Portland, Oregon
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Very nice, thank you very much!


  •  

    Posting Permissions

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