Can someone help me, I make a checkbox
<input type="checkbox" name="Agree" value="1">
How can I validate if the checkbox is not checked ?
So user have to checked it first if they want to continue.
Thanks.
| SitePoint Sponsor |

Can someone help me, I make a checkbox
<input type="checkbox" name="Agree" value="1">
How can I validate if the checkbox is not checked ?
So user have to checked it first if they want to continue.
Thanks.
Last edited by jet_wang; Aug 20, 2001 at 04:45.



I've done my checks to see whether the value for attribute "checked" is equal to true or false. For example:
function checkIt() {
var myVar = document.myForm.Agree.checked;
if(myVar == true) {
document.write("box is checked");
}
else {
document.write("box is empty");
}
}
I think that the true/false are booleans so they do not have to be in quotation marks but I could be wrong. Also, I work primarily with IE so I don't know how netscape will handle this.
This script would be better:
And you'd use it like this: checkIt('myForm.myElement')Code:function checkIt(loc) { if(loc.checked == true) { alert(loc + ' is checked'); } else { alert(loc + ' is empty'); } }




Hi jet_wang,
I'm going to presume you want your validation to occur with the submission and the "cannot continue" means you want to kill the submission.
function validate(formObj)
{
isOk2Send = true;
for (i = 0; i < formObj.length; i++)
{
...some validation code on checkboxes above the critical one....
if ( (formObj.elements[i].type == 'checkbox') && (formObj.elements[i].name == 'Agree') )
{
if (! formObj.elements[i].checked)
isOk2Send = false;
}
...more validation if necessary....
}
if (isOk2Send)
formObj.submit();
else
alert("You must check Agree to continue.");
}
<form name='something' action='something.xxx' method='something'>
......html....
<input type='checkbox' name='Agree' value='1'>Agree
<input type='button' value='submit' onClick='validate(this.form)'>
</form>
Vinny
moderator at:Webxpertz Forums
THe only thing I have issue with here is:
<form name='something' action='something.xxx' method='something'>
......html....
<input type='checkbox' name='Agree' value='1'>Agree
<input type='button' value='submit' onClick='validate(this.form)'>
</form>
Change it to an onSubmit in the form and this will give you greater control over actual form submission, and means that if people don't hit <ENTER> the form will still get validated![]()




Hi Jeremy,
Either you misread something, or I am on one of my walkabouts in daze city.
How does my example get submitted if the isOk2Send is false? I do not have a submit button in the form; the submission is through the function.
Also, how does onSubmit give you greater control?
Vinny
moderator at:Webxpertz Forums
Ah, it's a type="button"...
So the user must now go to the effort of grabbing their mouse and going to the button you have specified when an onSubmit in the <form> would allow them to hit enter and still get validated information.
How does onSubmit give you greater control, besides the above? You are staying within user norms and you don't need to do a forced JS submit.
Also, the form still works if the user has JS turned off.




Hi Jeremy,
phew, for a minute I thought I was having a flashback
Norms are norms are norms; I look at it more as programmer's choice/style.You are staying within user norms and you don't need to do a forced JS submit.
Perhaps, but then any other javascripts on the page would not be executed either. And, in this day and age, I doubt if anyone other than a complete paranoid would have their javascript disabled.Also, the form still works if the user has JS turned off.
Peace. Different strokes for different folks.
Vinny
moderator at:Webxpertz Forums
Part of being a programmer is usability. Your ease of use is significantly decreased by requiring the users to click on a specific portion of the screen, versus allowing them to choose between doing that and hitting enter.
Yes, the rest of the JS wouldn't execute if the user had JS turned off. However, in your version how would such a user submit the form? In mine it still gets submitted.




Hi Jeremy,
from the original post:Part of being a programmer is usability. Your ease of use is significantly decreased by requiring the users to click on a specific portion of the screen,
The implication of the checkbox (and jet_wang's request) is that:<input type="checkbox" name="Agree" value="1">
1) the user must agree to continue
2) the user has scrolled to the position where the checkbox is -- after having read x amount of text -- otherwise, there is no way the user would have any need to agree or disagree.
3) most people use their mouse to scroll
4) once the mouse is in hand, it is generally used to click on a checkbox
5) most web sites do not automatically tab or place the focus on a checkbox
6) 'significantly' ??????
7) I personally would use buttons for agree/disagree and use them in the manner I indicated.
8) this is getting to be more of a design argument, and costing me much more time and effort than I care to expend. jet_wang has a number of options open to him/her, perhaps more than s/he wants. Let sleeping dogs lie.
Vinny
moderator at:Webxpertz Forums
Bookmarks