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!
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!