I've been playing with this form validation method for a while and have
tried an array of things but haven't had any luck with a couple items.
1. The validateForm() function doesn't detect when the Min price is
greater than the maximum price.
2. The validateForm() function doesn't call isNumber() on the minBaths,
maxBaths, minBeds, or maxBeds.
3. Despite errors in input and the validateForm() function returning
false, the form is still submitted.
These are probably obvious fixes...I'm just not seeing them. thanks for
any help.
Here's the code:
<html>
<head>
<title></title>
<link rel="stylesheet " href="css/main.css">
<script language="JavaS cript">
<!--
function validateForm(th isForm){
alert("validate Form() called");
//alert("'" + thisForm.minBat hs.value + "'");
//alert(isNumber( thisForm.minBat hs.value));
if (thisForm.minPr ice.value == "") {
alert("You must enter a starting price.");
thisForm.minPri ce.focus();
return false;
} else if(!isNumber(th isForm.minPrice .value)) {
alert("Please enter a number for the starting price.");
thisForm.minPri ce.focus();
return false;
} else if (thisForm.maxPr ice.value == "") {
alert("You must enter a maximum price.");
thisForm.maxPri ce.focus();
return false;
} else if(!isNumber(th isForm.maxPrice .value)) {
alert("Please enter a number for the maximum price.");
thisFormForm.ma xPrice.focus();
return false;
} else if(thisForm.min Price.valueOf() >
thisForm.maxPri ce.valueOf()) {
alert("The starting value is greater than the maximum price.");
thisForm.minPri ce.focus();
return false;
} else if(!isNumber(th isForm.minBaths .value)) {
alert("Please enter a number for the minimum bathrooms.");
thisForm.minBat hs.focus();
return false;
} else if(!isNumber(th isForm.maxBaths .value)) {
alert("Please enter a number of the maximum bathrooms.");
thisForm.maxBat hs.focus();
} else if(!isNumber(th isForm.minBeds. value)) {
alert("Please enter a number for the minimum bedrooms.");
thisForm.minBed s.focus();
return false;
} else if(!isNumber(th isForm.maxBeds. value)) {
alert("Please enter a number for the maximum bedrooms.");
thisForm.maxBed s.focus();
return false;
} else {
return false;
}
}
function isNumber(entry) {
//alert("isNumber () Called");
validChar='0123 456789'; // characters allowed
strlen=entry.le ngth; // how long is test string
if (strlen < 1) {return false;} // no check!
// Now scan string for illegal characters
for (i=0; i < strlen; i++ ) {
if (validChar.inde xOf(entry.charA t(i)) < 0) {
return false;
}
}
//alert("returnin g true");
return true;
}
//-->
</script>
</head>
<body>
<!-- HEADER START -->
<table border="0" width="100%" cellpadding="0" cellspacing="0" >
<tr>
<td class="bottombo rder" colspan="3">
<div align="right" style="font-size:12px;">| <a
href="/">Home</a> |
<a href="/about.php">Abou t</a> |
<a href="/contact.php">Co ntact</a> | </div>
</td>
</tr>
<!-- HEADER END -->
<tr>
<td valign="top" width="550">
<form name="searchFor m" onSubmit="retur n validateForm(th is)"
method="post">
<input type="hidden" name="advanced" value="true" />
<input type="hidden" name="valid" value="true" />
<table width="600" cellspacing="1" cellpadding="1" border="0"
class="padLeft" >
<tr>
<td nowrap align="right">S treet:</td>
<td colspan="7" nowrap> &n bsp; <inpu t
type="text" name="StreetNam e" size="30" value=""/></td>
</tr>
<tr>
<td nowrap align="right">C ity:</td>
<td nowrap colspan="4">&nb sp; <input
type="text" name="City" size="20"
value=""/> &n bsp;   ; State:
&nb sp;
<select name="State">
<option value="NC">NC</option>
<option value="SC">SC</option>
</select>
<span
style="padding-left:6px;paddin g-right:6px;font-size:12px;font-weight:bold;col or:$999";>-OR-</span>
Zip Code: &nbs p; <input type="text" name="ZipCode" size="6"
maxlength="5" value=""/></td>
</tr>
<tr>
<td colspan="5">&nb sp;</td>
</tr>
<td colspan="5" style="padding-left:10px">
<table border="0" cellpadding="1" cellspacing="1" >
<tr>
<td nowrap align="right">M in Price:</td>
<td nowrap> &n bsp; <inpu t type="text"
name="minPrice" size="4" value="120000"/></td>
<td nowrap align="right">M ax Price:</td>
<td nowrap> &n bsp; <inpu t type="text"
name="maxPrice" size="4" value="125000"/></td>
</tr>
<tr>
<td nowrap align="right">M in Beds:</td>
<td nowrap> &n bsp; <inpu t type="text"
name="minBeds" size="4" maxlength="2" /></td>
<td nowrap align="right">M ax Beds:</td>
<td nowrap> &n bsp; <inpu t type="text"
name="maxBeds" size="4" maxlength="2" /></td>
</tr>
<tr>
<td nowrap align="right">M in Baths:</td>
<td nowrap> &n bsp; <inpu t type="text"
name="minBaths" size="4" maxlength="2" value="ax" /></td>
<td nowrap width="75" align="right">M ax Baths:</td>
<td nowrap> &n bsp; <inpu t type="text"
name="maxBaths" size="4" maxlength="2" /></td>
</tr>
</table>
</td>
</tr>
<tr>
<td> </td>
<td colspan="3"><in put type="submit" value="Submit"
name="submit" /></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>