I created a contact form on my website and marked the name, phone and email address as required fields using a javascript form I found online. It works just fine in Firefox but not in IE and I can’t figure out what I need to change to make it work. Help!! Or if anyone has a better script I am open to changing it. I had to include a recapthca so maybe that’s what is messing things up. It is targeting contactengine.php in the action because I used a separate page for the packaging of the email and the recapthca php.
Here is the java I used..
<script type=”text/javascript” language=”JavaScript”><!–
var FormName = “form”;
var RequiredFields = “Name,Phone,Email”;
function ValidateRequiredFields()
{
var FieldList = RequiredFields.split(“,”)
var BadList = new Array();
for(var i = 0; i < FieldList.length; i++) {
var s = eval(‘document.’ + FormName + ‘.’ + FieldList[i] + ‘.value’);
s = StripSpacesFromEnds(s);
if(s.length < 1) { BadList.push(FieldList[i]); }
}
if(BadList.length < 1) { return true; }
var ess = new String();
if(BadList.length > 1) { ess = ‘s’; }
var message = new String(‘nnThe following field’ + ess + ‘ are required:n’);
for(var i = 0; i < BadList.length; i++) { message += ‘n’ + BadList[i]; }
alert(message);
return false;
}
function StripSpacesFromEnds(s)
{
while((s.indexOf(‘ ‘,0) == 0) && (s.length> 1)) {
s = s.substring(1,s.length);
}
while((s.lastIndexOf(‘ ‘) == (s.length – 1)) && (s.length> 1)) {
s = s.substring(0,(s.length – 1));
}
if((s.indexOf(‘ ‘,0) == 0) && (s.length == 1)) { s = ”; }
return s;
}
// –>
</script>
Here is my form..
<form name=form method=”post” action=”contactengine.php” onsubmit=”return ValidateRequiredFields();”>
<table>
<tr>
<td class=”left”><label for=”Name”>Name:</label></td>
<td><input type=”text” name=”Name”/></td>
</tr>
<tr>
<td class=”left”><label for=”Phone”>Phone:</label></td>
<td><input type=”text” name=”Phone”/></td>
</tr>
<tr>
<td class=”left”><label for=”Email”>Email:</label></td>
<td><input type=”text” name=”Email”/></td>
</tr>
<tr>
<td class=”left”><label for=”Message”>Message:</label></td>
<td><textarea name=”Message” rows=”20″ cols=”20″></textarea></td>
</tr>
<tr>
<td> </td>
<td>
<div id=”captcha-area”>
<?php
require_once(‘recaptchalib.php’);
$publickey = “6LdZ5woAAAAAAHfphS7H04kkIm2xbV0tTJKJQfY9”;
$privatekey = “6LdZ5woAAAAAAN9GSclnkj8ZgPLeH_Bz2Ce2zo9t”;
# the response from reCAPTCHA
$resp = null;
# the error code from reCAPTCHA, if any
$error = null;
# are we submitting the page?
if ($_POST[“submit”]) {
$resp = recaptcha_check_answer ($privatekey,
$_SERVER[“REMOTE_ADDR”],
$_POST[“recaptcha_challenge_field”],
$_POST[“recaptcha_response_field”]);
if ($resp->is_valid) {
echo “You got it!”;
# in a real application, you should send an email, create an account, etc
} else {
# set the error code so that we can display it. You could also use
# die (“reCAPTCHA failed”), but using the error message is
# more user friendly
$error = $resp->error;
}
}
echo recaptcha_get_html($publickey, $error);
?>
</div>
</td>
</tr>
<tr>
<td> </td>
<td>
<input type=”submit” name=”submit” value=”Submit” class=”submit-button” />
</td>
</tr>
</form>
</table>