Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating Checkboxes 1

Status
Not open for further replies.

AlpineKJB

Programmer
Dec 15, 2000
17
US

I am trying to validate a checkbox (ie4). But I have ran into an anomaly with checkboxes. The javascript code below always returns "on" . Even if I UNcheck the box. Can someone help?

-Ken

<HTML>
<HEAD>
<SCRIPT>
function myform_onsubmit() {
var str;
str = document.myform.readit.value;
alert(str);
}
</SCRIPT>
</HEAD>

<BODY>
<FORM
name=myform
method=post
action=&quot;register.asp&quot;
onsubmit=&quot;return myform_onsubmit()&quot;>
<input type=checkbox name=&quot;readit&quot;> Yes, I read the Terms and Conditions below.<br>
</FORM>
</BODY></HTML>
 
HI AlpineKJB,

it should be &quot;str = document.myform.readit.checked;&quot; not str = document.myform.readit.value

and it should return &quot;true&quot; if check, and &quot;false&quot; if uncheck

hope this helps, Chiu Chan
cchan@emagine-solutions.com
 
IE, at least, seems to have a problem with checkbox and radio button values. It doesn't seem to see them correctly, although they do get passed to the server OK. The only way I've found that works reliably is to do like it says above and check the &quot;checked&quot; attritbute. For radio buttons, since all within a group have the same name, you have to use form.name[0].checked for the first radio button in a group, form.name[1].checked for the second, etc.
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
ts, i don't think it's a problem, it's a &quot;normal&quot; behaviour : if they are not checked, they are not passed. Even if YOU would like another behaviour, it's the standard one ! ;-)
 
That may be true, but if they ARE checked, and you have specified a value, then that value is passed as the value of the checkbox/radio button (otherwise the value is &quot;ON&quot;). That being the case then the control DOES have a value, so why can't javascript access it? Just seems to make sense to me. Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
as far as i know, even when checked with a value, they only retrun on (or 1 or true) or off (or 0 or false)
 
Not true. They will return to your cgi program whatever value you have specified in the value= attribute. For instance, if you have the following two radio buttons, the value of the field &quot;IsRep&quot; will be either &quot;Y&quot; or &quot;N&quot;, depending on which radio button is checked.
Code:
<input type=&quot;radio&quot; name=&quot;IsRep&quot; value=&quot;Y&quot;> Yes &nbsp;
<input type=&quot;radio&quot; name=&quot;IsRep&quot; value=&quot;N&quot;> No &nbsp;
The same basic thing applies to checkboxes. Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top