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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Radio button Validating

    I am trying to validate that a radio button has been selected. I have incorporated this into a function that validates all the inputs on the page and I can't see what is wrong. The radio buttons are named "prctype". Please help.

    <script type="text/javascript" language="javascript">
    function validate(f){
    var el, e = 0;while (el = f.elements[e++]) {
    if (el.name == 'FirstName' && !el.value) {alert('Please fill in your First Name.');el.focus();return false;}
    else if (el.name == 'LastName' && !el.value) {alert('Please fill in your Last Name.');el.focus();return false;}
    else if (el.name == 'Address1' && !el.value) {alert('Please fill in your Street Address.');el.focus();return false;}
    else if (el.name == 'City' && !el.value) {alert('Please fill in your City');el.focus();return false;}
    else if (el.name == 'State' && !el.value) {alert('Please fill in your State');el.focus();return false;}
    else if (el.name == 'Zip' && !el.value) {alert('Please fill in your Zip');el.focus();return false;}
    else if (el.name == 'Country' && !el.value) {alert('Please fill in your Country (ex. USA).');el.focus();return false;}
    else if (el.name == 'HomePhone1'&& !el.value) {alert('Please fill in your Area Code (ex. 999).');el.focus();return false;}
    else if (el.name == 'HomePhone2'&& !el.value) {alert('Please fill in your Phone Exchange (ex. 555).');el.focus();return false;}
    else if (el.name == 'HomePhone3'&& !el.value) {alert('Please fill in your Phone Number (ex. 1234).');el.focus();return false;}
    else if (el.name == 'Make' && !el.value) {alert('Please fill in the Make of your Vehicle.');el.focus();return false;}
    else if (el.name == 'Model' && !el.value) {alert('Please fill in the Model of your Vehicle.');el.focus();return false;}
    else if (el.name == 'Year' && !el.value) {alert('Please fill in the Year of your Vehicle (ex. 2004).');el.focus();return false;}
    else if (el.name == 'ccname' && !el.value) {alert('Please fill in your name exactly as it appears on your Credit Card.');el.focus();return false;}
    else if (el.name == 'ccnum' && !el.value) {alert('Please fill in your Credit Card number.');el.focus();return false;}
    else if (enrollfm.elements.spares.options.selectedIndex == 0) {alert('Please choose an option from the Vehicle drop-down list, then submit again!');return false;}
    else if (enrollfm.elements.ccexpmo.options.selectedIndex == 0) {alert('Please select the Month that your credit card expires, then submit again!');return false;}
    else if (enrollfm.elements.ccexpyr.options.selectedIndex == 0) {alert('Please select the Year that your credit card expires, then submit again!');return false;}
    else
    var x=0;
    for (var i=0;i < document.enrollfm.prctype.length; i++){
    if (document.enrollfm.prctype[i].checked){x++}
    }
    if(x==0){alert ("Please choose a Subscription Plan");return false}
    }
    }return true;}
    </script>

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Your else needs brackets.

    You don't need this big long code to loop if you are hard-coding the element names like that. It defeats the purpose.
    A loop is for when you don't know the names, like dynamically generated elements and the like. Otherwise just directly use the names. Much cleaner.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

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