...

View Full Version : Terms Checkbox Validation



airpirate
07-23-2012, 07:46 PM
I don't use Javascript much, but would like to learn it. I have a website that I want to validate a form, check for missing required fields and check to make sure the user checks a checkbox agreeing to the Terms and Conditions when registering.

I found a useful script that works well when checking for missing fields, but does not work on the checkbox agreeing to the terms. :(

I know very little about Javascript so any help showing me how to make sure the checkbox for the terms is checked before submitting the form would be very much appreciated.

Here is the script I found online that works well other than on the checkbox



<script language="JavaScript">
<!--

/***********************************************
* Required field(s) validation script
***********************************************/

function formCheck(formobj){
// Enter name of mandatory fields
var fieldRequired = Array("fname", "lname", "email", "phone", "terms");
// Enter field description to appear in the dialog box
var fieldDescription = Array("First Name", "Last Name", "Email Address", "Phone Numbers", "Terms and Conditions");
// dialog message
var alertMsg = "Please complete the following fields:\n";

var l_Msg = alertMsg.length;

for (var i = 0; i < fieldRequired.length; i++){
var obj = formobj.elements[fieldRequired[i]];
if (obj){
switch(obj.type){
case "select-one":
if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "select-multiple":
if (obj.selectedIndex == -1){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "text":
case "textarea":
if (obj.value == "" || obj.value == null){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
default:
}
if (obj.type == undefined){
var blnchecked = false;
for (var j = 0; j < obj.length; j++){
if (obj[j].checked){
blnchecked = true;
}
}
if (!blnchecked){
alertMsg += " - " + fieldDescription[i] + "\n";
}
}
}
}

if (alertMsg.length == l_Msg){
return true;
}else{
alert(alertMsg);
return false;
}
}
// -->
</script>

<form id="form1" name="form1" method="post" action="process.php" onsubmit="return formCheck(this);">


Thank you in advance!

WolfShade
07-23-2012, 08:10 PM
If there is only one checkbox with that name:


if(document.forms["formname"].checkBoxName.checked) { //code for true
}
else { //code for false
}

airpirate
07-23-2012, 09:00 PM
Thanks. I used your suggestion and re-wrote a script that works :)



<script language="JavaScript">
<!--
function validateForm()
{
var x=document.forms["form1"]["fname"].value;
if (x==null || x=="")
{
alert("First Name must be filled out");
return false;
}

var x=document.forms["form1"]["lname"].value;
if (x==null || x=="")
{
alert("Last Name must be filled out");
return false;
}

var x=document.forms["form1"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Please enter valid e-mail address");
return false;
}

var x=document.forms["form1"]["phone"].value;
if (x==null || x=="")
{
alert("A valid Phone Number is required");
return false;
}

if(document.forms["form1"].terms.checked) { return true;
}
else { alert("Please Read and Acknowledge that you Agree to the Terms and Conditions");
return false;
}

}


// -->
</script>

Old Pedant
07-23-2012, 09:42 PM
And if you don't KNOW if there is one or many ahead of time, test for it:


var obj = formobj.elements[fieldRequired[i]];
if (obj){
if ( obj.length != null )
{
// must be multiple checkboxes, radio buttons, or <select multiple>
for ( var n = 0; n < obj.length; ++n )
{
var subobj = obj[n];
switch( subobj.type )
{
... etc. ...
}
}
} else {
// must be a single element
switch(obj.type){
case "checkbox":
if ( obj.checke ) { ... } else { ... }
break;
case ...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum