PDA

View Full Version : Mail form validation - not working with CGI script



Franco50
07-04-2006, 07:31 AM
Required field(s) validation

http://www.dynamicdrive.com/dynamicindex16/requiredcheck.htm

I have previously used this script successfully in conjunction with a FormMail.asp script but am now having problems getting it to work with a CGI mail form. The form is sent whether all required fields are completed or not, with no prompt to complete them. Is there some reason why it will not work with a CGI script? I have copied the HTML from the mailing page below and would appreciate any assistance.

-----------------------------------------------------------------------------------------------------

<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", "Company", "Business_Type", "Telephone", "Email_Address");
// Enter field description to appear in the dialog box
var fieldDescription = Array("Your Name", "Company Name", "Type of Business", "Contact Telephone", "email Address");
// dialog message
var alertMsg = "Sorry, you must 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 method="POST" action="http://greenplanetpr.co.uk/cgi-bin/mailer/mailer.cgi">
<form name="formcheck" onsubmit="return formCheck(this);">
<input type="hidden" name="Redirect" value="http://greenplanetpr.co.uk/thanks.htm">
<input type="hidden" name="To" value="robert@greenplanetpr.co.uk">
<p><small><font face="Verdana">Your name:<font color="#FF0000">*</font><br>
<input type="text" name="Name" size="35" tabindex="1">
</font></small></p>
<p><font face="Verdana" style="font-size: smaller">Company </font><small>
<font face="Verdana">name:<font color="#FF0000">*</font><br>
<input type="text" name="Company" size="35" tabindex="2">
</font></small></p>
<p><font face="Verdana" style="font-size: smaller">Type of business</font><small><font face="Verdana">:<font color="#FF0000">*</font><br>
<input type="text" name="Business_Type" size="40" tabindex="3">
</font></small></p>
<p><font face="Verdana" style="font-size: smaller">Contact Telephone</font><small><font face="Verdana">:<font color="#FF0000">*</font><br>
<input type="text" name="Telephone" size="24" tabindex="4">
</font></small></p><p><small><font face="Verdana">Your email address:<font color="#FF0000">*</font><br>
<input type="text" name="EMail_Address" size="35" tabindex="5">
<br><br>Your message:<br>
<textarea rows="7" name="Message" cols="40" tabindex="6"></textarea>
<br><br>
<input type="submit" value="Send message">
</font></small></p>
</form>

Twey
07-04-2006, 12:52 PM
It doesn't help that you've nested two forms and only closed one of them. I think you mean:
<form onsubmit="return formCheck(this);" name="formcheck" method="POST" action="http://greenplanetpr.co.uk/cgi-bin/mailer/mailer.cgi">Your code leaves quite a lot to be desired. Have you heard of CSS?

Franco50
07-04-2006, 06:10 PM
Thanks for the help, your suggestion fixed the problem - I could have done without the sarcasm though. :)

I am not a professional web designer and all the code you saw was obtained elsewhere and not written by me - the top part obviously from Dynamic Drive and the mailform part from my web host who offer common web scripts.

Twey
07-04-2006, 06:17 PM
I could have done without the sarcasm though. :)Just helping you improve your coding style :p
I am not a professional web designerIf you're creating pages, that makes you a web designer. Professional or no, you'd do well to at least read through the W3Schools HTML (http://www.w3schools.com/html/) and CSS (http://www.w3schools.com/css/) tutorials.