M
Matt
I want to compare the address number in javascript, and the address
number
is alphanumeric. I have a text box and the user needs to enter the
number between 2 numbers as follows (e.g. Please enter the address
number between N11800 and N12800). Note that N11800 and N12800 are
dynamic, it can be pure integers. But this is just an example.
The bug is if the user enter a number that is an integer, for example,
111,
then it still consider as good number. But if I entered A333, then it
has error.
I know I can comment out the following, so that just pure string
comparison. But it sometimes doesn't work.
//if (!isNaN(strValue) && strValue != '')
// strValue = parseInt(strValue);
I guess the wrong I really have no idea what's going on.
Please help. Thanks!!
<html>
<head>
<script type="text/javascript">
function checkField()
{
var startNumber = "N11800";
var endNumber = "N12800";
var strValue = document.InputForm.txtNumber.value;
alert("You entered = " + strValue);
if (!isNaN(strValue) && strValue != '')
strValue = parseInt(strValue);
//just pure string comparisons
if ( strValue == '' || (strValue < startNumber || strValue >
endNumber)){
alert("Please enter a number between " + startNumber + " and " +
endNumber);
InputForm.txtNumber.value = "";
InputForm.txtNumber.focus();
return false;
}
else
{ alert("good number");
return true;
}
}
</script>
</head>
<body>
<FORM NAME="InputForm">
<P>Please enter the address number between N11800 and N12800:
<input type="text" name="txtNumber">
<P><input type="button" value="check field" onClick="checkField()">
</form>
</body>
</html>
number
is alphanumeric. I have a text box and the user needs to enter the
number between 2 numbers as follows (e.g. Please enter the address
number between N11800 and N12800). Note that N11800 and N12800 are
dynamic, it can be pure integers. But this is just an example.
The bug is if the user enter a number that is an integer, for example,
111,
then it still consider as good number. But if I entered A333, then it
has error.
I know I can comment out the following, so that just pure string
comparison. But it sometimes doesn't work.
//if (!isNaN(strValue) && strValue != '')
// strValue = parseInt(strValue);
I guess the wrong I really have no idea what's going on.
Please help. Thanks!!
<html>
<head>
<script type="text/javascript">
function checkField()
{
var startNumber = "N11800";
var endNumber = "N12800";
var strValue = document.InputForm.txtNumber.value;
alert("You entered = " + strValue);
if (!isNaN(strValue) && strValue != '')
strValue = parseInt(strValue);
//just pure string comparisons
if ( strValue == '' || (strValue < startNumber || strValue >
endNumber)){
alert("Please enter a number between " + startNumber + " and " +
endNumber);
InputForm.txtNumber.value = "";
InputForm.txtNumber.focus();
return false;
}
else
{ alert("good number");
return true;
}
}
</script>
</head>
<body>
<FORM NAME="InputForm">
<P>Please enter the address number between N11800 and N12800:
<input type="text" name="txtNumber">
<P><input type="button" value="check field" onClick="checkField()">
</form>
</body>
</html>