/    Sign up×
Community /Pin to ProfileBookmark

Validating form witch radios

I have made working validation of form:
[url]www.itssimple.pl/1.html[/url]
And then I’ve tried to make the same validation but in form witch radios. And my problem is that it isn’t working at all. Have a look:
[url]www.itssimple.pl/5.html[/url]

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@FangAug 24.2010 — [CODE]function validateRadio(n) { // pass radio group name
var aRadio = document.getElementsByName(n);
for(var i=0; i<aRadio.length; i++) {
if(aRadio[i].checked) {
return true;
}
return false;
}[/CODE]
Copy linkTweet thisAlerts:
@heldanauthorAug 24.2010 — Ok, but how to came through all radios using for?
Copy linkTweet thisAlerts:
@heldanauthorAug 24.2010 — I know how does the for works, but I dont know how to get throught all radios and validate answers if I can not name them the same way.

Look into my code, and you will know, what i want to do.
Copy linkTweet thisAlerts:
@FangAug 24.2010 — The groups have [I]name[/I]s: no1 - no10
Copy linkTweet thisAlerts:
@heldanauthorAug 24.2010 — And how to get throught all groups using one for?
Copy linkTweet thisAlerts:
@FangAug 24.2010 — [CODE]validate() {
var aNames = ['doe', 'ray', 'me', 'fa'];
for(var i=0; i<aNames[i].length; i++) {
if(!validateRadio(aNames[i])) {
alert('Group '+aNames[i]+' not checked');
return false;
}
}
return true;
}
[/CODE]
Copy linkTweet thisAlerts:
@heldanauthorAug 24.2010 — Ok, but you defined all groups. I want to have dynamicly generated number of questions. That is my problem.
Copy linkTweet thisAlerts:
@FangAug 24.2010 — The dynamically created names can be pushed in the array.
Copy linkTweet thisAlerts:
@heldanauthorAug 24.2010 — I have corrected it the way you showed me and it still isn't working.

link:www.itssimple.pl/5.html
Copy linkTweet thisAlerts:
@FangAug 25.2010 — Incorrect referencing of elements and arrays
[CODE]function validate() {
errfound = false;
var odp = new Array(0,0,1,1,2,2,3,3,0,0); // arry numbering starts at 0 not 1
var odpa = new Array("A","A","B","B","C","C","D","D","A","A");

var brak = "<img src="x.jpg"> <span style="color:red">Brak odpowiedzi,</span><span style="color:green"> poprawnie:</span> ";
var lub = " lub ";
var br = "<br/>";
var bledy = "";
var poprawnie = "<img src="x.jpg"><span style="color:red">&#172;le,</span> <span style="color:green">poprawnie:</span> ";
var blad = new Array();
var punkty = 0;

var no = ['no1', 'no2', 'no3', 'no4', 'no5', 'no6', 'no7', 'no8', 'no9', 'no10'];
for(var j=0;sel=document.getElementsByName(no[j]), j<sel.length; j++) {

if (sel[odp[j]].checked == true) { blad[j] = "<img src="tick.jpg"> <br/>";punkty++;}
else if(sel[0].checked == false && sel[1].checked == false && sel[2].checked == false && sel[3].checked == false) {blad[j] = brak + odpa[j] + br;}
else {blad[j] = poprawnie + odpa[j] + br; }
}


[/CODE]

else {blad[[COLOR="Red"]h[/COLOR]] = poprawnie There is no variable 'h', changed to 'j' in script
×

Success!

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