PDA

View Full Version : Accept Terms script (alert)



Motions
09-05-2007, 04:51 PM
"Accept terms" form submission
http://www.dynamicdrive.com/dynamicindex16/acceptterm.htm

I want to know if this script can be changed to pop up an alert box rather than disabling the submit button. I have custom backgrounds on my buttons and there's nothing visible to know it's disabled. People try clicking on it and nothing happens and they don't know why.

An alert warning would make more sense. I see in the code there is an alert, but not sure what it's for.

Scott

ddadmin
09-05-2007, 08:19 PM
Sure, you can try the below version instead, which does away with disabling the submit button, and just pops up an alert message if the checkbox isn't checked upon submission:


<script>

//"Accept terms" form submission- By Dynamic Drive
//For full source code and more DHTML scripts, visit http://www.dynamicdrive.com
//This credit MUST stay intact for use

function defaultagree(el){
if (el.agreecheck.checked)
return true
else{
alert("Please read/accept terms to submit form")
return false
}
}

</script>

<body>

<form name="agreeform" onSubmit="return defaultagree(this)">
Rest of your form here<br>
<input name="agreecheck" type="checkbox"><b>I agree to the above terms</b><br>
<input type="Submit" value="Submit!">
</form>

<script>
//change two names below to your form's names
document.forms.agreeform.agreecheck.checked=false
</script>

Motions
09-05-2007, 09:08 PM
That did it somewhat. I had to do some modifications to get it to work with Required fields validation. Here's my code if anyone wants to use these together.


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

/***********************************************
* Required field(s) validation v1.10- By NavSurf
* Visit Nav Surf at http://navsurf.com
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function formCheck(formobj){
// Enter name of mandatory fields
var fieldRequired = Array("name", "email", "phone");
// Enter field description to appear in the dialog box
var fieldDescription = Array("Contact Name", "Email", "Phone");
// 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){
if (el.policy.checked)
return true;
else{
alert("Please read/accept terms to submit form");
return false;
}
}else{
alert(alertMsg);
return false;
}
}
-->
</script>