/    Sign up×
Community /Pin to ProfileBookmark

Urgent, need help with validating radio buttons

Hi, i have a form whereby people select values via radio buttons. I can’t seem to validate it before submitting the values into my php file ready to be written to my C2.txt file. The following code is my form1.html file:

[CODE]
<html>
<head>
<title> form 1</title>
<script language=”JavaScript”>
<!–
function validate_radio(frm)
{
alert(“inside function”);

var i;
for(i=0; i < document.form1.select[i].length; i++) // enter for loop, if i is less than the number of radio b
uttons, increment i
{

if (document.form1.select[i].checked)

{
return true;

}
else
{
alert(“You did not select a value”);
return false;
}

}

}
–>
</script>
</head>
<body>
<center>
<form name = “form1″ method=”post” action=”handler4.php”>
Mr.X <input type=”radio” name=”select” value=”Mr.X “> <br />
Tom Cruis <input type=”radio” name=”select” value=”Tom Cruis ” ><br />

<input type=”submit” name=”submit” value=”send” onClick=”return validate_radio(form1)”;>
</form>
</center>
</body>
</html>
[/CODE]

At the moment, when i press submit, it goes to my php file without validating it. Please can somebody tell me why and how to adjust it before going into my php file as this is very URGENT ?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@ricpDec 25.2006 — For a start, don't pass form1 as it is, either pass it as a string ("form1") and then pick up the element via document.getElementById or better still, use (this.form). IE may recognise form1 as a variable reference to an ID'ed element on the page, no other browser will though.

You could also cut down the amount of code in that loop with a simple while that would take care of both the for() and the if() at the same time.
Copy linkTweet thisAlerts:
@nvidiaauthorDec 25.2006 — I tried to add a while loop but it still goes onto my php site without validing it.
[CODE]
<script language="JavaScript">
<!--
function validate_radio(frm)
{
alert("inside function");
var i = 0;
while ( i < document.form1.select[i].checked) // enter for loop, if i is less than the number of radio button
s, increment i
{
return true;
i++;

else
{

alert("You did not select a value");
i++;
return false;
}

}

}
-->
</script>
</head>
<body>
<center>
<form name = "form1" method="post" action="handler4.php">

Mr.X <input type="radio" name="select" value="Mr.X "> <br />
Tom Cruis <input type="radio" name="select" value="Tom Cruis " ><br />

<input type="submit" name="submit" value="send" onClick="return validate_radio(this.form1)";>
[/CODE]


Can somebody help me please as i would be very greatful
Copy linkTweet thisAlerts:
@ricpDec 25.2006 — I said to you in the reply to your PM to use (this.form) and not (this.form1)

<i>
</i>// html
&lt;form&gt;
&lt;input type="radio" name="myRadios" value="a"/&gt;
&lt;input type="radio" name="myRadios" value="b"/&gt;
&lt;input type="submit" onclick="return validateRadio(this.form)"/&gt;
&lt;/form&gt;


<i>
</i>// js
function validateRadio(myForm) {
var radioGroup = myForm.myRadios
var radioGroupSelected = false;
for (var radIdx=0;radIdx&lt;myRadios.length&amp;&amp;!radioGroupSelected;radIdx++) {
if (myRadios[radIdx].checked) radioGroupSelected = true;
}
if (!radioGroupSelected) alert("Please select an option");
return radioGroupSelected;
}


Now if you were only validating one thing (the radios), then you could pass (this.form.myRadio) to the validateRadio function.
×

Success!

Help @nvidia 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 5.23,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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