-
November 30th, 2012, 02:08 PM
#1
Validate a checkbox
I have created a form that requires checking a box certifying that you agree to terms.
Code:
<input name="certify" type="checkbox" value="Y" ID="CertChkbox"/>
This is the last item on the form which contains mostly address info. I then added the following javascript:
Code:
<script>
function validateForm()
{
var x=document.forms["PledgeForm"]["FirstName"].value;
if (x==null || x=="")
{
alert("First name is a required field and must be completed");
return false;
}
var x=document.forms["PledgeForm"]["LastName"].value;
if (x==null || x=="")
{
alert("Last name is a required field and must be completed");
return false;
}
this goes on for a few more fields and then I tried adding the following code for the checkbox:
if (PledgeForm.Certify.value == null)
{
alert("error message");
}
I am not sure what I am doing wrong. How can I validate the checkbox?
Thanks in advance.
Last edited by PeejAvery; December 1st, 2012 at 09:54 PM.
Reason: Added code tags
-
December 1st, 2012, 09:57 PM
#2
Re: Validate a checkbox
First off, never do client-side form processing if security is an issue at all. Anyone can disable or change JavaScript through their browser. The server-side scripts cannot be altered by the client.
Second, if you're going to stick with JavaScript, checkboxes just return either true or false. It's as simple as that!
Code:
if (document.forms["PledgeForm"]["Certify"].checked) {...}
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|