I am finding that my JS validation script isn’t working in Firefox (but does in IE) since I changed the field names to begin with a letter. Anyone know why?
I have form fields that look like this:
<input type="text" name="a_Name" size="18" />
<input type="text" name="b_Email" size="18" />
The form action tag looks like this:
<form action="../gdform.php " method="post" name="Guestbook" id="Guestbook" onsubmit="this.c_Company.optional=true;this.d_Street.optional=true;this.e_Suite.optional=true;this.f_City.optional=true;this.g_State.optional=true;this.h_Country.optional=true;this.i_Zipcode.optional=true;this.j_WorkPhone.optional=true;return verify(this);">
And the javascript validation script looks like this:
// A utility function that returns true if a string contains only
// whitespace characters
function isblank(s) {
for(var i = 0; i < s.length; i++) {
var c = s.charAt(i);
if ((c != ' ') && (c != '
') && (c != ' ')) return false;
}
return true;
}
// This is the function that performs form verification. It is invoked
// from the onsubmit event handler. The handler should return whatever
// value this function returns.
function verify(f) {
var msg;
var empty_fields = "";
var errors = "";
// Loop through the elements of the form, looking for all Text and
// Textarea elements that don't have an "optional" property defined.
// Then check for fields that are empty and make a list of them. Also, if
// any of these elements have a "min" or a "max" property defined, verify
// that they are numbers and are in the right range. If the element has a
// "numeric" property defined, verify that it is a number, but don't check
// its range. Put together error messages for fields that are wrong.
for(var i = 0; i < f.length; i++) {
var e = f.elements*;
if (((e.type == "text") || (e.type == "textarea")) && !e.optional) {
// First check if the field is empty
if ((e.value == null) || (e.value == "") || isblank(e.value)) {
empty_fields += "
" + e.name;
continue;
}
// Now check for fields that are supposed to be numeric
// Example: onSubmit="this.Zipcode.numeric=true;return verify(this);"
if (e.numeric) {
var v = parseFloat(e.value);
if (isNaN(v)) {
errors += "- Zipcode needs to be a number.
";
}
if (e.value.length < 5) {
errors += "- You have entered an invalid Zipcode.
";
}
}
}
}
var checkEmail = Guestbook.b_Email.value
if ((checkEmail.indexOf('@') < 0) || ((checkEmail.charAt(checkEmail.length-4) != '.') && (checkEmail.charAt(checkEmail.length-3) != '.')))
{
errors += "- You have entered an invalid email address.
";
Guestbook.b_Email.select();
//return false;
}
if (Guestbook.i_Zipcode.value.length < 5) {
errors += "- You have entered an invalid Zipcode.
";
}
if (Guestbook.g_State.selectedIndex == 0) {
errors += "- Please select a State or Province.
";
returnStatus = 0;
};
if (Guestbook.h_Country.selectedIndex == 0) {
errors += "- Please select a Country.";
returnStatus = 0;
};
// Now, if there were any errors, display the messages, and
// return false to prevent the form from being submitted.
// Otherwise, return true.
if (!empty_fields && !errors) return true;
msg = "___________________________________________________
"
msg += "The form was not submitted because of the following error(s).
";
msg += "Please correct these error(s) and re-submit.
";
msg += "___________________________________________________
"
if (empty_fields) {
msg += "- The following required field(s) are empty:"
+ empty_fields + "
";
if (errors) msg += "
";
}
msg += errors;
alert(msg);
return false;
}
Adding the letter to the beginning of the field name helps with a PHP script problem - it alpha orders the field names.