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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Aug 2006
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts

    validate radiobutton

    hi


    i have 3 radio buttons and 3 textboxes on a form.



    <input type="radio" name="radiogroup1" value="others"><b>Other Needs</b>
    <input type="radio" name="radiogroup1" value="courses"><b>Need Courses</b>
    <input type="radio" name="radiogroup1" value="money">
    <b>Need Money</b>

    Enter Name<input type="text" name="ur_name">
    Enter Father Name<input type="text" name="father_name">
    Enter Your Age<input type="text" name="age">

    what i want is if a user clicks on money radiobutton and enter his name and father name and without filling the age textbox press submit button then it display alert to fill the age textbox.if clicks any of other two radiobuttons,dont display any alert.any of your help will be surely appreciated.

    iam using the following code to validate the radiobutton(if a user dont clicks any radiobutton display alert to select it)
    but i dont know how i can check that when money radiobutton is clicked the age textbox should be filled.it should not be blank


    thanks
    mrjameer

    <script language="javascript">
    function validate()
    {
    var i=0;
    var chkcount;
    chkcount=0;
    for (i=0;i<document.frm.elements.length;i++)
    {
    if ( document.frm.elements[i].type=="radio" && document.frm.elements[i].name=="radiogroup1" && document.frm.elements[i].checked==true)
    {
    chkcount=chkcount+1
    }
    }
    if (chkcount==0)
    {
    alert(" Please Select Your Need");
    return false;
    }
    return true;
    }
    </script>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,895
    Thanks
    202
    Thanked 2,530 Times in 2,508 Posts
    Try this:-

    <FORM name = "frm">

    <input type="radio" name="radiogroup1" value="others"><b>Other Needs</b><br>
    <input type="radio" name="radiogroup1" value="courses"><b>Need Courses</b><br>
    <input type="radio" name="radiogroup1" value="money"><b>Need Money</b><br>

    Enter Name<input type="text" name="ur_name">
    Enter Father Name<input type="text" name="father_name">
    Enter Your Age<input type="text" name="age"><br><br>

    <input type = "button" name = "valbuttton" value = "Click to proceed" onclick = "validate()">

    <script language="javascript">
    function validate() {
    var i=0;
    var chkcount= 0;

    for (i=0;i<document.frm.elements.length;i++) {
    if ( document.frm.elements[i].type=="radio" && document.frm.elements[i].name=="radiogroup1" && document.frm.elements[i].checked==true) {
    chkcount=chkcount+1;
    }
    }

    if (chkcount == 0) {
    alert(" Please Select Your Need");
    return false;
    }

    if ((document.frm.radiogroup1[2].checked) && (document.frm.age.value == "")) {
    alert ("Age required");
    return false;
    }

    return true;
    }
    </script>

    </FORM>

    You need additional validation to check that the entries into the textboxes are sensible (e.g. age is a number within a sensible range).

  • #3
    Regular Coder
    Join Date
    Aug 2006
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hi Philip M

    thanks for your reply.

    i want to put a submit button instead of normal button.

    so that what ever the information entered is stored in mysql table.how i can replace the normal button with submit button(<input type=submit name=submit>).

    thanks
    mrjameer.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,895
    Thanks
    202
    Thanked 2,530 Times in 2,508 Posts
    <input type = "submit" name = "submitbutton" value = "Click to submit" onclick = "return validate(this)">

    Do not give your submit button the name 'submit'. It can confuse the browser.


  •  

    Posting Permissions

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