SitePoint Sponsor |
|
User Tag List
Results 1 to 11 of 11
Thread: Checkbox
-
Aug 20, 2001, 01:39 #1
- Join Date
- Jun 2001
- Location
- Jakarta
- Posts
- 156
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Validate Checkbox
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.
-
Aug 20, 2001, 05:32 #2
-
Aug 21, 2001, 05:47 #3
- Join Date
- Jun 2001
- Location
- rollin' on dubs
- Posts
- 492
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Aug 21, 2001, 06:20 #4
- Join Date
- Jun 2001
- Location
- Toronto, Canada
- Posts
- 9,123
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
This script would be better:
Code:function checkIt(loc) { if(loc.checked == true) { alert(loc + ' is checked'); } else { alert(loc + ' is empty'); } }
-
Aug 21, 2001, 09:06 #5
- Join Date
- Feb 2000
- Location
- where the World once stood
- Posts
- 700
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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>
VinnyWhere the World Once Stood
the blades of grass
cut me still
-
Aug 21, 2001, 09:08 #6
- Join Date
- Jun 2001
- Location
- Toronto, Canada
- Posts
- 9,123
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
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
-
Aug 21, 2001, 09:32 #7
- Join Date
- Feb 2000
- Location
- where the World once stood
- Posts
- 700
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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?
VinnyWhere the World Once Stood
the blades of grass
cut me still
-
Aug 21, 2001, 09:40 #8
- Join Date
- Jun 2001
- Location
- Toronto, Canada
- Posts
- 9,123
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Aug 21, 2001, 09:53 #9
- Join Date
- Feb 2000
- Location
- where the World once stood
- Posts
- 700
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi Jeremy,
phew, for a minute I thought I was having a flashback
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.
Peace. Different strokes for different folks.
VinnyWhere the World Once Stood
the blades of grass
cut me still
-
Aug 21, 2001, 09:56 #10
- Join Date
- Jun 2001
- Location
- Toronto, Canada
- Posts
- 9,123
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Aug 21, 2001, 10:24 #11
- Join Date
- Feb 2000
- Location
- where the World once stood
- Posts
- 700
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi Jeremy,
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,
<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.
VinnyWhere the World Once Stood
the blades of grass
cut me still
Bookmarks