/    Sign up×
Community /Pin to ProfileBookmark

Radio Button Validation

I tried making in such a way that an error msg s prompt out if user select the two of the often radio button. but it doesnt seems to be working.

[code=php]
<script language=”JavaScript”>
function Validate()
{
var count=0;

if (myform[‘question1’][1].checked) {count++}

if (myform[‘question2’][1].checked) {count++}

if(count == 2)
{
alert(“Please select an answer.”)
}

return (count!=2);

}
</script>
<body>
<form name=”myform” action=”newminutes.php” method=”POST” onsubmit=”return validate()”>
<input type=”radio” name=”question1″ id=”1b1″ value=”1″>Always</input>
<input type=”radio” name=”question1″ id=”1b2″ value=”2″>Often</input>
<input type=”radio” name=”question1″ id=”1b3″ value=”3″>Sometimes</input>
<input type=”radio” name=”question1″ id=”1b4″ value=”4″>Rarely</input>
<input type=”radio” name=”question1″ id=”1b5″ value=”5″>Never</input>

<input type=”radio” name=”question2″ id=”1b1″ value=”1″>Always</input>
<input type=”radio” name=”question2″ id=”1b2″ value=”2″>Often</input>
<input type=”radio” name=”question2″ id=”1b3″ value=”3″>Sometimes</input>
<input type=”radio” name=”question2″ id=”1b4″ value=”4″>Rarely</input>
<input type=”radio” name=”question2″ id=”1b5″ value=”5″>Never</input>
<div>
<input type=”submit” name=”action” value=”Submit Answer”>
</div>
</form>
</body>

[/code]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@NedalsOct 07.2005 — 
  • 1. function Validate() should be function validate()

  • 2. return (count!=2); ????

    Need to return 'true' if ok and 'false' on error

  • 3. Remember the array starts at '0'

  • 4. missing </head>
  • Copy linkTweet thisAlerts:
    @winsonleeauthorOct 07.2005 — i am using a script for my menu bar on my page. something i dont understand is without the menubar script, my validate does work but with the menubar script, my validate doesnt work. both are different script and i dont understand how each script can affect the other.

    works...

    [code=html]
    <script language="JavaScript">
    function validate()
    {
    var count=0;

    if (myform['attandance0'][1].checked) {count++}


    if (myform['attandance1'][1].checked) {count++}


    if (myform['attandance2'][1].checked) {count++}


    if (myform['attandance3'][1].checked) {count++}


    if(count == 4)
    {
    alert("Please select an answer.")
    return false
    }

    return true

    }
    </script>
    [/code]


    Doesnt work...

    [code=html]

    <SCRIPT >

    var arrList = new Array()
    arrList[0] = new Array("1","30JAN2005")
    arrList[1] = new Array("2","30DEC2005")
    arrList[2] = new Array("3","01JAN2005")
    arrList[3] = new Array("4","24JAN2005")
    arrList[4] = new Array("5","25JAN2005")
    arrList[5] = new Array("6","26JAN2005")
    arrList[6] = new Array("7","29JUL2005")
    arrList[7] = new Array("8","29JAN2005")
    arrList[8] = new Array("9","09FEB2005")
    arrList[9] = new Array("10","10FEB2005")
    arrList[10] = new Array("11","11FEB2005")
    arrList[11] = new Array("12","11JAN2005")
    arrList[12] = new Array("13","")
    arrList[13] = new Array("14","")





    //Main Menu items:
    menus[0] = new menu(1.5, "horizontal", 7.2,3.8, -2, -2, "#CACAFF", "#0000A0", "Verdana,Helvetica", 9,
    "bold", "bold", "black", "gray", 0, "gray", 0.2, "rollover:images/tri-down1.gif:images/tri-down2.gif", false, true, true, true, 12, false, 0.4, 0.4, "black");
    menus[0].addItem("http://www.dynamicdrive.com", "", 8, "center", "Home", 0);
    menus[0].addItem("newminutes.php", "", 8, "center", "New Minutes", 0);
    menus[0].addItem("#", "", 8, "center", "Read Minutes", 1);
    menus[0].addItem("logout.php", "", 8, "center", "Logout", 0);

    //Sub Menu for 2nd Main Menu Item ("web building"):
    menus[1] = new menu(10, "vertical", 0, 0, 0, 0, "#CACAFF", "#0000A0", "Verdana,Helvetica", 9, "bold",
    "bold", "black", "gray", 0, "gray", 0.2, "rollover:images/tri-right1.gif:images/tri-right2.gif", false, true, false, false, 0, false, 0.4, 0.4, "black");
    menus[1].addItem("http://www.dynamicdrive.com", "", 1.5, "left", "Complete Minutes", 2);
    menus[1].addItem("http://www.javascriptkit.com", "", 1.5, "left", "Action Items", 3);
    menus[1].addItem("http://www.codingforums.com", "", 1.5, "left", "People Present", 4);
    menus[1].addItem("http://www.builder.com", "NEWWIN", 1.5, "left", "Issues Discussed", 5);


    //Sub Menu for Sub Menu "Sports News":
    menus[2] = new menu(8, "vertical", 0, 0, 0, 0, "#E1E1E1", "gray", "Verdana,Helvetica", 9, "bold", "bold", "gray", "gray", 0.1, "gray", 0.2, 62, false, true, false, false, 0, false, 0.4, 0.4, "gray");
    menus[2].addItemx("#", "", 8, "right", "hello", 0, arrList,"complete");

    menus[3] = new menu(8, "vertical", 0, 0, 0, 0, "#E1E1E1", "gray", "Verdana,Helvetica", 9, "bold", "bold", "gray", "gray", 0.1, "gray", 0.2, 62, false, true, false, false, 0, false, 0.4, 0.4, "gray");
    menus[3].addItemx("#", "", 8, "right", "hello", 0, arrList,"action");

    menus[4] = new menu(8, "vertical", 0, 0, 0, 0, "#E1E1E1", "gray", "Verdana,Helvetica", 9, "bold", "bold", "gray", "gray", 0.1, "gray", 0.2, 62, false, true, false, false, 0, false, 0.4, 0.4, "gray");
    menus[4].addItemx("#", "", 8, "right", "hello", 0, arrList,"people");

    menus[5] = new menu(8, "vertical", 0, 0, 0, 0, "#E1E1E1", "gray", "Verdana,Helvetica", 9, "bold", "bold", "gray", "gray", 0.1, "gray", 0.2, 62, false, true, false, false, 0, false, 0.4, 0.4, "gray");
    menus[5].addItemx("#", "", 8, "right", "hello", 0, arrList,"issue");
    </SCRIPT>



    <script language="JavaScript">
    function validate()
    {
    var count=0;

    if (myform['attandance0'][1].checked) {count++}


    if (myform['attandance1'][1].checked) {count++}


    if (myform['attandance2'][1].checked) {count++}


    if (myform['attandance3'][1].checked) {count++}


    if(count == 4)
    {
    alert("Please select an answer.")
    return false
    }

    return true

    }
    </script>
    [/code]
    Copy linkTweet thisAlerts:
    @NedalsOct 07.2005 — ...my validate does work but with the menubar script, my validate doesnt work.[/QUOTE]Have you tried the menubar script WITHOUT the validate script. I think you will find there are problems with the menubar script. Solve those and both scripts should work together.

    Where did you get that menubar script? There seems to be some missing code.
    ×

    Success!

    Help @winsonlee spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.27,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...