/    Sign up×
Community /Pin to ProfileBookmark

Validation of radio buttons

Hi

I need to validate whether or not a radio button has been selected using javascript.
I have been using this:

[CODE]function valbutton(thisform) {
myOption = -1;
for (i=0; i<thisform.radiobutton.length; i++) {
if (thisform.radiobutton[i].checked) {
myOption = i;
}
}
if (myOption == -1) {
alert(“Please Select one of the Radio Buttons”);
return false;
}
thisform.submit(); // this line submits the form after validation
}[/CODE]

This works fine, but I need something that will be able to do the same with radio buttons that are named as an array. Example, my set of radio buttons that I want to validate are all called ” radiobutton[2]”.

I tried changing the above script to use this new name, but it no longer works.

Anyone know how I can do this?

Thanks

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@FangOct 18.2005 — <i>
</i>for (var i=0; i&lt;thisform.elements['radiobutton[2]'].length; i++) {
if (thisform.elements['radiobutton[2]'][i].checked) {
alert(i);
}
}
Copy linkTweet thisAlerts:
@thegoonerauthorOct 18.2005 — <i>
</i>for (var i=0; i&lt;thisform.elements['radiobutton[2]'].length; i++) {
if (thisform.elements['radiobutton[2]'][i].checked) {
alert(i);
}
}
[/QUOTE]


EXCELLENT!

Thanks for your time..... REALLY appreciated!

One last thing...

I want to loop a function through all my sets of radio buttons, so how would I loop through a form that contained 2 or more sets of radio buttons, named radiobutton[1] and radiobutton[2] etc. I would like to loop it as if I didn't know how many sets of radio buttons there were, using a "for" loop if possible.

How would that work with your code above.

Many thanks for this!!
Copy linkTweet thisAlerts:
@FangOct 18.2005 — For radiobutton[1] and radiobutton[2] :
function val(thisform) {
for (var r=1; r&lt;3; r++) {
for (var i=0; i&lt;thisform.elements['radiobutton['+r+']'].length; i++) {
if (thisform.elements['radiobutton['+r+']'][i].checked) {
alert('radiobutton['+r+'] : index'+i);
}
}
}
}
Copy linkTweet thisAlerts:
@thegoonerauthorOct 18.2005 — For radiobutton[1] and radiobutton[2] :
function val(thisform) {
for (var r=1; r&lt;3; r++) {
for (var i=0; i&lt;thisform.elements['radiobutton['+r+']'].length; i++) {
if (thisform.elements['radiobutton['+r+']'][i].checked) {
alert('radiobutton['+r+'] : index'+i);
}
}
}
}
[/QUOTE]



I can't thank you enough FANG.

That's solved my problem.

If I'm ever in Holland I'll be sure to get you a beer!

Cheers
×

Success!

Help @thegooner 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.7,
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,
)...