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 6 of 6
  1. #1
    New Coder
    Join Date
    Jul 2002
    Posts
    88
    Thanks
    0
    Thanked 0 Times in 0 Posts

    MY Validation code doesn't work for radiobuttons - why?

    This is my very simple validation script for an ASP form. It works (and thanks due here to you all for helping me 2 years ago) fine, but I suddenly now find that it will not work for a radiobutton.

    Why?

    Here's the script I'm trying to get to work...for a trio of radio buttons

    <script LANGUAGE="JavaScript">
    <!-- HIDE FROM OTHER BROWSERS

    function validate(outputForm)
    if (outputForm.radioValue.value == "")
    {

    alert("The Value of the Truck is required. Thank you.");
    outputForm.radioValue.focus();
    return (false);
    }
    return (true);
    // STOP HIDING FROM OTHER BROWSERS -->
    </script>

    ???

    Jim

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Are you checking to see if the radio is selected? then you want
    Code:
    if (!outputForm.radioValue.checked)
    {
        //do something
    }

  • #3
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Yes, a radio button or checkbox's value does not change (generally) when it is selected. It is whether or not it is selected that you want to check. Which the code above does just fine. Just thought I'd explain a bit further.

    When you execute

    var x=document.form.checkbox.checked;

    x holds the value of either true or false.

    so now you can do
    Code:
    if(x==true)
    {
       alert("Selected");
    }
    else
    {
      alert("Not selected");
    }
    Basscyst
    Last edited by Basscyst; 12-10-2004 at 01:58 AM.
    Helping to build a bigger box. - Adam Matthews

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    trio of radio buttons
    If they have all the same name (to be able to let only a single posibility to check), than to reach one button you have to circle as through an array, as the

    document.forms[0].elements['radioname'] is a collection of elements, not a single element.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    New Coder
    Join Date
    Jul 2002
    Posts
    88
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I think I understand...but is this correct?

    Okay, I get it, I think. It's not correct to check for a "value" -- I have to check that a selection was made....

    So here's the script, now modified to do that -- right?

    function validate(outputForm)
    if (outputForm.radioValue.checked== "")
    {
    alert("A Truck Value Selection is required. Thank you.");
    outputForm.radioValue.focus();
    return (false);
    }
    return (true);

    Will that work? And won't it work for ANY number of radiobuttons, 2 or 20?

    Jim

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    You have not read my replay. And you have to look after the boolean checked, not after the value.

    PHP Code:
    var document.forms[0].elements['radioValue'];//the radio buttons as a collection
    var param 0;
    for(var 
    i=0;i<r.length;i++){
    if (
    r[i].checked){param++}
    }
    if(
    param==0){
    alert("A Truck Value Selection is required. Thank you.");
    return 
    false
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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