/    Sign up×
Community /Pin to ProfileBookmark

Form Radio Validation

I’m trying to validate a Radio-selection, in a loop.
Is it my loop that’s wrong,
or the way I reference the Form?


<script type=”text/javascript>
function checkMonthChoice()
var check = 0; // fields 0-5 = Months 1-6
for ( var k=0 ; k<6 ; k++ )
if ( document.this.field[k].checked )
check = k+1; // save monthNum1
if ( !check )
alert (“No Month selected”);
return false;
return check;


<form name=’edit’ method=’post’
onsubmit=’return chekMonthChoice(this)’

<td colspan=’3′><h3>Which Month?</h3></td>
<td><input type=’radio’ value=1 name=’emnth’>January</td>
<td><input type=’radio’ value=2 name=’emnth’>February</td>
<td><input type=’radio’ value=3 name=’emnth’>March</td>
<td><input type=’radio’ value=4 name=’emnth’>April</td>
<td><input type=’radio’ value=5 name=’emnth’>May</td>
<td><input type=’radio’ value=6 name=’emnth’>June</td>
<td><input type=’reset’ value=’Reset’></td>
<td> &nbsp; </td>
<td><input type=’submit’ value=’Submit Month’></td>


Thanks, in advance, for any help.

to post a comment

16 Comments(s)

Copy linkTweet thisAlerts:
@CharlesNov 14.2006 — I think that you want:&lt;script type="text/javascript&gt;
function checkMonthChoice (f) {
var check = false, e, i = 0
while (e = f.emnth[i++]) {if (e.checked) check = true}
if ( !check ) {
alert ("No Month selected");
return false;
return check;
Copy linkTweet thisAlerts:
@JimJauthorNov 14.2006 — Thanks Charles, for a really positiuve reply.

I replaced my script with yours.

When I click a Month, and click Submit,

I get a 404 Error with this message

(/cgi-bin/file_boxes) for does not exist.

But I'm learning a lot.

I see you passed argument (f) to script.

What is f?

You declared 3 variables.

You increment 'i' but do not appear to use it.

Is that so?

Glad to see how you used name 'emnth'.


Copy linkTweet thisAlerts:
@sovikNov 14.2006 — Try this:

</i>function checkMonthChoice() {
var form=window.document.edit;
for (var b = 0; b &lt; window.document.edit.elements['emnth'].length; b++) {
if (form.elements['emnth'][b].checked) { var foo=1; }

<i> </i>}
if (foo!=1) { alert ("Error"); } else { form.submit(); }

And I dont know if it a really good idea to call this function onsubmit action. But if it work, good 4 u.
Copy linkTweet thisAlerts:
@JimJauthorNov 14.2006 — Thanks Sovik,

What am I doing wrong?

As before I substituted your script for the other,

Clicked a Month, clicked Submit.

Got the same 404:

Error 404 - file not found

What does that mean ? -

well exactly that !

the page you are looking (/cgi-bin/file_boxes) for does not exist

Copy linkTweet thisAlerts:
@CharlesNov 14.2006 — I don't pass an argument "f" so much as you pass a reference to the form itself, using "this", and I catch it and call it "f".

My loop, [font=monospace]while (e = f.emnth[i++]) {}[/font], is a cute little short hand. It runs until the assignment returns false and each iteration it assigns to "e" the next item in the arrray. When it runs out of items it returns false. "i++" is called a "post decriment". After the expression is evaluated its value is increased by one.
Copy linkTweet thisAlerts:
@JimJauthorNov 14.2006 — Sovic,

Is that the answer -

your hint that I ought not to call the checking function ?

Do I just leave it there, and let CGI use it ??

Thanks for the hint.

Copy linkTweet thisAlerts:
@sovikNov 14.2006 — [qoute]the page you are looking (/cgi-bin/file_boxes) for does not exist[/qoute]

That mean that the file you submit your form to does not exist. Check the path, file extension, permisions.


Call your button button like <input type="button" name="anything exept submit" value="whatever" onclick="functionhere()">
Copy linkTweet thisAlerts:
@JimJauthorNov 14.2006 — Thanks Charles,

I hadn't noticed the f.emth[i++].

(I reckon I'm a good C programmer, though obviously a bit scatterbrained!)

Would also like your response to my second reply to Sovic.


Copy linkTweet thisAlerts:
@mjdamatoNov 14.2006 — IN Charles' script, f is the form object. You could just as easily define the form object inside the function. Depending on how you call the function one or the other may be preferred. Here is the same code with some minor changes - all you need to do is specifie the field object

&lt;script type="text/javascript&gt;
function checkMonthChoice () {

var i = 0;
field = document.[I]formname.fieldname[/I];
while (field[i]) {
if (field[i].checked) return true;
alert ("No Month selected");
return false;
Copy linkTweet thisAlerts:
@CharlesNov 14.2006 — Sovic is assuming that you are calling the validation from a button, the script then submits the form. This is very bad. Keep going the way you are.

The 404 error has nothing to do with this page. The server can't find the cgi script. Do you have the name correct?
Copy linkTweet thisAlerts:
@sovikNov 14.2006 — Hey guys dont you have "K" on your keyboard? ?

Can you explain whats wrong with submiting a form via js submit function?
Copy linkTweet thisAlerts:
@JimJauthorNov 14.2006 — Charles & soviik,

I think I now understand my problem.

I've never used a Form before,

never used Radios or Checks.

I coded this some days ago, only just tested it.

When I came to code <FORM METHOD= ACTION=

I took a guess at a CGI name.

I'm testing offline, on home computer.

What CGI ought I to code?

Copy linkTweet thisAlerts:
@JimJauthorNov 14.2006 — Thanks mjdamato,

Like your terse code.

Have substituted it, but not going to test it yet,

till I find out how to code the CGI bit.


Copy linkTweet thisAlerts:
@sovikNov 14.2006 — JimJ if you dont have any web server installed on your computer, then basiclly you can not do much with your form.

The page where you do submit your form (it can be the current page as well) have to pull out the information which user had provided in the form. This can be done by any server side language like VBS (ASP) PHP etc. But to do so you need the WEB server installed (IIS, Apache, etc).
Copy linkTweet thisAlerts:
@JimJauthorNov 14.2006 — I installed Apache some weeks ago,

but have not used it yet.

Ought I be referring to 'localhost'

or something like that?

Copy linkTweet thisAlerts:
@JimJauthorNov 14.2006 — I installed Apache some weeks ago, but have not used it yet.

That means I have loads of questions:

1 In addressing the server, I gather I need to refer to myself as 'localhost'

or something like that ??

2 If I'm not using a remote server, I presume I do not need to use a server-side language?

3 Can I then use javascript as my 'cgi' script?

4 How do I refer to it?



Help @JimJ 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 10.4,
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: @ddiebold17,
tipped: article
amount: 1000 SATS,

tipper: @Mqlinka19,
tipped: live stream
amount: 4900 SATS,

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