Forum Moderators: open

Message Too Old, No Replies

Javascript form validation

How to add a checkbox

         

wheelie34

1:59 pm on Feb 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi
My first post here in Javascript forum so please be gentle ;) I have a form that gets checked before posting, I am trying to add a checkbox that will also need a tick before proceeding, heres the bit I think needs changing


var tempobj=which.elements[i];
if (tempobj.name.substring(0,8)=="required") {
if (((tempobj.type=="text"¦¦tempobj.type=="textarea")&&
tempobj.value=='')¦¦(tempobj.type.toString().charAt(0)=="s"&&
tempobj.selectedIndex==0)) {
pass=false;
break;

I have tried changing textarea to checkbox but it allows the submission with or without a tick as the value isn't picked up, the checkbox name is requiredterms, the word required gets caught by the validation script, any ideas? as usual the forum broke the pipes, they are pipes.

Thanks in advance

mehh

3:05 pm on Feb 16, 2008 (gmt 0)

10+ Year Member



That looks far too complex for such a simple task. Try this:
[pre]function checkForm(){
var formId ="formID";
var checkID="submit";
return document.forms[formId][checkID].checked;
}[/pre]
[pre]<form id="formID" onsubmit="return checkForm();">
<input type="checkbox" name="submit">
<input type="submit" value="Submit">
</form>[/pre]

wheelie34

3:36 pm on Feb 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi

Its not just for a checkbox, theres plenty of other elements it currently checks, I just need to add a checkbox to be validated.

I have this snippet


(tempobj.type=="checkbox")
{
eval(tempobj.checked=0)
}

How/where do I add it to the above javascript so it gets validated?

Edit

Ok worked it out, just added this line to catch the checkbox


¦¦
(tempobj.type=="checkbox"&&tempobj.checked==0)

So it now looks like

((tempobj.type=="text"¦¦tempobj.type=="textarea")&&tempobj.value=='')
¦¦
(tempobj.type.toString().charAt(0)=="s"&&tempobj.selectedIndex==0)
¦¦
(tempobj.type=="checkbox"&&tempobj.checked==0)
)

Works as expected

[edited by: wheelie34 at 4:17 pm (utc) on Feb. 16, 2008]

mehh

4:15 pm on Feb 16, 2008 (gmt 0)

10+ Year Member



eval(tempobj.checked=0)

That will uncheck the box not see if it is checked. Going back to your origainal code, after some headache I think I understand it. Presuambly you are looping through the form's eleements and seeing if is filled in, meaning you will have to add something to this line:
if (((tempobj.type=="text" ¦¦ tempobj.type=="textarea") &&
tempobj.value=='') ¦¦ (tempobj.type.toString().charAt(0)=="s" &&
tempobj.selectedIndex==0))

So this new if statment should work:
if (
((tempobj.type=="text" ¦¦ tempobj.type=="textarea" )&&tempobj.value=='') ¦¦ //For text areas
(tempobj.type.toString().charAt(0)=="s" && tempobj.selectedIndex==0)¦¦ //For selects
(tempobj.type=="checkbox" && tempobj.checked) //For checkboxes
)

wheelie34

4:22 pm on Feb 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ha beat you to it

so I don't need the checked==0 just checked?

just tested without ==0 and it allows it through, so, it seems my code was correct, am I right?

mehh

5:00 pm on Feb 16, 2008 (gmt 0)

10+ Year Member



ha beat you to it

actualy I posted 2mins before you :p.
so I don't need the checked==0 just checked?

checked should return a boolean anyway, so the ==0 to make it a boolean is pointless. The ==0 is ok but it is a waste of space and makes the code less readable. As you said it works though