Forum Moderators: open
it validates in FF but not IE7...
can anyone please help me?
function validateInputs() {
var controls = [
{id:'fname', label:'First Name'},
];
for (var i = 0; i < controls.length; i++) {
var c = document.getElementById(controls[i].id);
if (c.value.length < 1) {
alert('Please enter a value for ' + controls[i].label + '.');
c.focus();
return false;
}
}
return true;
}
function validateSelect() {
var controls = [
{id:'nhm_associate', label:'NHM Agent'},
{id:'lead_type', label:'Lead Type'},
{id:'community', label:'Community Interested In'}
];
for (var i = 0; i < controls.length; i++) {
var c = document.getElementById(controls[i].id);
if (c.selectedIndex == 0) {
alert('Please select a ' + controls[i].label + '.');
c.focus();
return false;
}
}
return true;
}
function validateForm() {
// Check input values
if (!validateInputs()) {
return false;
}
// Check drop downs
if (!validateSelect()) {
return false;
}
return true;
}
var controls = [
{id:'fname', label:'First Name'},
];
You have an extra comma in there for no reason and therefore the browser is checking for another object but isn't finding it.
To fix this, it should be changed to the following, just removing the comma:
var controls = [{id:'fname', label:'First Name'}];
Next, you should be comparing values with zero using === not just ==, so change to the following:
if (c.selectedIndex === 0) {
Try that first and see what you get.
should be more careful with my code...
Now...new issue.
the drop down selections are as follows:
<select name="community" id="community">
<option value="N/A" selected>N/A</option>
<option value="Sonata">Sonata</option>
<option value="Hazelwood Village">Hazelwood Village</option>
<option value="RockHampton">RockHampton</option>
<option value="Newcastle Heights">Newcastle Heights</option>
<option value="Dashwood">Dashwood</option>
<option value="Rosecroft">Rosecroft</option>
<option value="The Cliffs">The Cliffs</option>
</select>
If the user trys to submit without selecting an option, it does prompt the user to select a "community", that works great..
..BUT, once you hit "OK" from the prompt screen, it automatically submits with the selected value of "N/A"...
how can essentially say, in the script, value can not be NA?
becuase even if i remove the property value="N/A", i go through all of the other fields, and once i get to "community" it seems to still be able to submit.
I just noticed as well, then when the prompt comes up for the "community" it says "Error on Page" with the yellow exclamation point in the lower left corner of the browser...
function validateSelect() {
var controls = [
{id:'nhm_associate', label:'NHM Agent'},
{id:'lead_type', label:'Lead Type'},
{id:'community', label:'Community Interested In'}
];
var errCnt = 0;
for (var i = 0; i < controls.length; i++) {
var c = document.getElementById(controls[i].id);
if (c.selectedIndex == 0) {
errCnt++;
alert('Please select a ' + controls[i].label + '.');
c.focus();
}
}
if(errCnt > 0){
return false;
}else{
return true;
}
}
Here's the full test I just did and it work with both FX and IE.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Keywords" content="">
<meta name="Description" content=""><script type="text/javascript">
function validateInputs() {
var controls = [
{id:'fname', label:'First Name'}
];
for (var i = 0; i < controls.length; i++) {
var c = document.getElementById(controls[i].id);
if (c.value.length < 1) {
alert('Please enter a value for ' + controls[i].label + '.');
c.focus();
return false;
}
}
return true;
}function validateSelect() {
var controls = [
{id:'nhm_associate', label:'NHM Agent'},
{id:'lead_type', label:'Lead Type'},
{id:'community', label:'Community Interested In'}
];
for (var i = 0; i < controls.length; i++) {
var c = document.getElementById(controls[i].id);
if (c.selectedIndex === 0) {
alert('Please select a ' + controls[i].label + '.');
c.focus();
return false;
}
}
return true;
}
function validateForm() {
// Check input values
if (!validateInputs()) {
return false;
}
// Check drop downs
if (!validateSelect()) {
return false;
}
return true;
}
</script></head>
<body><form method="post" action="http://www.google.com" onsubmit="return validateForm()">
<input type="text" id="fname"><br><select id="nhm_associate">
<option value="N/A" selected>N/A</option>
<option value="Sonata">Sonata</option>
<option value="Hazelwood Village">Hazelwood Village</option>
<option value="RockHampton">RockHampton</option>
<option value="Newcastle Heights">Newcastle Heights</option>
<option value="Dashwood">Dashwood</option>
<option value="Rosecroft">Rosecroft</option>
<option value="The Cliffs">The Cliffs</option>
</select><br><select id="lead_type">
<option value="N/A" selected>N/A</option>
<option value="Sonata">Sonata</option>
<option value="Hazelwood Village">Hazelwood Village</option>
<option value="RockHampton">RockHampton</option>
<option value="Newcastle Heights">Newcastle Heights</option>
<option value="Dashwood">Dashwood</option>
<option value="Rosecroft">Rosecroft</option>
<option value="The Cliffs">The Cliffs</option>
</select><br><select id="community">
<option value="N/A" selected>N/A</option>
<option value="Sonata">Sonata</option>
<option value="Hazelwood Village">Hazelwood Village</option>
<option value="RockHampton">RockHampton</option>
<option value="Newcastle Heights">Newcastle Heights</option>
<option value="Dashwood">Dashwood</option>
<option value="Rosecroft">Rosecroft</option>
<option value="The Cliffs">The Cliffs</option>
</select><br><input type="submit">
</form></body>
</html>