wbg976
10-02-2005, 03:53 PM
I setup a javascript function to validate a form submission. the low price should be lower than the high price otherwise it throws up and alert box. In the example below (on my computer at least), I get an error box when I hit submit... what is wrong???
<html>
<head>
</head>
<body>
<table border="0" width="100%" cellpadding="0" cellspacing="0">
<tr>
<td width="600" valign="top">
<script language="JavaScript">
<!--
function validateForm(thisForm){
if (thisForm.low.value == "") {
alert("You must enter a starting price.");
thisForm.low.focus();
return false;
} else if(!isNumber(thisForm.low.value)) {
alert("Please enter a number for the starting price.");
thisForm.low.focus();
return false;
} else if (thisForm.high.value == "") {
alert("You must enter a maximum price.");
thisForm.high.focus();
return false;
} else if(!isNumber(thisForm.high.value)) {
alert("Please enter a number for the maximum price.");
thisForm.low.focus();
return false;
} else if(thisForm.low.value > thisForm.high.value) {
alert("The starting value is greater than the maximum price. " + thisForm.low.value + " > " + thisForm.high.value);
thisForm.low.focus();
return false;
} else {
return true;
}
}
function isNumber(entry) {
validChar='0123456789'; // characters allowed
strlen=entry.length; // 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.indexOf(entry.charAt(i)) < 0) {
return false;
}
}
return true;
}
//-->
</script>
<form method="post" onSubmit="return validateForm(this)" action="test.html">
<input type="hidden" name="valid" value="true" />
<input type="hidden" name="start" value="0" />
<table border="0" cellpadding="1" cellspacing="1" width="100%">
<tr>
<td nowrap align="right">Price Range:</td>
<td nowrap> <input type="text" name="low" size="4" value="1999"/> to <input type="text" name="high" size="4" value="10000"/></td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" name="search" value="Search" />
</td>
</tr>
</table>
</form>
</body>
</html>
<html>
<head>
</head>
<body>
<table border="0" width="100%" cellpadding="0" cellspacing="0">
<tr>
<td width="600" valign="top">
<script language="JavaScript">
<!--
function validateForm(thisForm){
if (thisForm.low.value == "") {
alert("You must enter a starting price.");
thisForm.low.focus();
return false;
} else if(!isNumber(thisForm.low.value)) {
alert("Please enter a number for the starting price.");
thisForm.low.focus();
return false;
} else if (thisForm.high.value == "") {
alert("You must enter a maximum price.");
thisForm.high.focus();
return false;
} else if(!isNumber(thisForm.high.value)) {
alert("Please enter a number for the maximum price.");
thisForm.low.focus();
return false;
} else if(thisForm.low.value > thisForm.high.value) {
alert("The starting value is greater than the maximum price. " + thisForm.low.value + " > " + thisForm.high.value);
thisForm.low.focus();
return false;
} else {
return true;
}
}
function isNumber(entry) {
validChar='0123456789'; // characters allowed
strlen=entry.length; // 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.indexOf(entry.charAt(i)) < 0) {
return false;
}
}
return true;
}
//-->
</script>
<form method="post" onSubmit="return validateForm(this)" action="test.html">
<input type="hidden" name="valid" value="true" />
<input type="hidden" name="start" value="0" />
<table border="0" cellpadding="1" cellspacing="1" width="100%">
<tr>
<td nowrap align="right">Price Range:</td>
<td nowrap> <input type="text" name="low" size="4" value="1999"/> to <input type="text" name="high" size="4" value="10000"/></td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" name="search" value="Search" />
</td>
</tr>
</table>
</form>
</body>
</html>