SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question with form fields

    Hello,

    I am still developing my javascript skills and have a question.

    Say I have two radio buttons with the same name but different value. Selecting one of the radios is required. There are two text fields and depending on which radio is selected, one of the two text fields would be required.

    So I have radios named "type" one value is "current" and one is "new".

    If new is selected, then the text field "pbname" is requireed. If new is selected then the text field "reqno" is required.

    I have spent about 5 hours scouring the net for examples, so if there is somewhere I can look, please help.

    Thanks!

    Jim

  2. #2
    Word Painter silver trophy Shyflower's Avatar
    Join Date
    Oct 2003
    Location
    Winona, MN USA
    Posts
    10,053
    Mentioned
    142 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by bpoppa
    Hello,

    I am still developing my javascript skills and have a question.

    Say I have two radio buttons with the same name but different value. Selecting one of the radios is required. There are two text fields and depending on which radio is selected, one of the two text fields would be required.

    So I have radios named "type" one value is "current" and one is "new".

    If new is selected, then the text field "pbname" is requireed. If new is selected then the text field "reqno" is required.

    I have spent about 5 hours scouring the net for examples, so if there is somewhere I can look, please help.

    Thanks!

    Jim
    Have you looked at The JavaScript Source?
    Linda Jenkinson
    "Say what you mean. Mean what you say. But don't say it mean." ~Unknown

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2003
    Location
    va
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <script language="JavaScript">
    function checkRadio(elem) {
    var isRadio=true;

    if(!elem[0].checked&&!elem[1].checked)
    isRadio=false;

    if(isRadio) {
    if(elem[0].checked)
    whatRequired="pbname";
    else
    whatRequired="reqno";
    if(myFrm[whatRequired].value=="")
    alert("you must enter something in "+whatRequired+" form");
    else
    myFrm.submit();
    }
    else
    alert("please check a radio button!");

    }
    </script>


    <table width="500" border="0" cellspacing="0" cellpadding="0">
    <form action="myactionpage.php" method="post" name="myFrm">
    <tr>
    <td>
    <input type="radio" name="myRadio" value="current">radio 1<br>
    <input type="radio" name="myRadio" value="new">radio 2
    </td>
    </tr>
    <tr>
    <td>
    <input type="text" name="pbname">pbname<br>
    <input type="text" name="reqno">reqno
    </td>
    </tr>
    <tr>
    <td><input type="button" name="Submit" value="Submit" onClick="checkRadio(myRadio)"></td>
    </tr>
    </form>
    </table>


    merry christmas!


Bookmarks

Posting Permissions

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