Matt wrote:
I posted this problem before but no reply. Now I repost again.
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.
<script type="text/javascript">
Number.prototype.isBetween = function(low, high) {
return (+low <= this && this <= +high);
}
String.prototype.isBetween = function(low, high) {
return (low <= this && this <= high);
}
String.prototype.isBetweenIgnoreCase(low, high) {
var thisIgnoreCase = this.toLowerCase();
low = (low != null ? (new String(low)).toLowerCase() : '\0xff');
high = (high != null ? (new String(high)).toLowerCase() : '');
return (low <= thisIgnoreCase && thisIgnoreCase <= high);
}
function test(f) {
var theValue = f.elements['theValue'].value;
var theLowerValue = f.elements['theLowerValue'].value;
var theHigherValue = f.elements['theHigherValue'].value;
var isBetweenLowerAndHigher;
if (isNaN(theValue)) {
isBetweenLowerAndHigher = theValue.isBetween(theLowerValue,
theHigherValue);
} else {
isBetweenLowerAndHigher = (+theValue).isBetween(theLowerValue,
theHigherValue);
}
if (isBetweenLowerAndHigher) {
alert(
theValue + ' is between ' +
theLowerValue + ' and ' +
theHigherValue
);
} else {
alert(
theValue + ' is not between ' +
theLowerValue + ' and ' +
theHigherValue
);
}
}
</script>
<form name="myForm">
Value 1: <input type="text" name="theLowerValue" value="N11800"><br>
Value 2: <input type="text" name="theHigherValue" value="N12800"><br>
Enter a value between Value 1 and Value 2: <input type="text"
name="theValue" value="">
<input type="button" value="Test" onclick="test(this.form);">
</form>
Note that "n11900" is _not_ between N11800 and N12800. If you need to do
the test case-insensitively then use String#isBetweenIgnoreCase() (or
simply modify String#isBetween() to be case-insensitive).
You can do the same thing with three functions:
function isBetweenNumbers(value, low, high) { ... }
function isBetweenStrings(value, low, high) { ... }
function isBetweenStringsIgnoreCase(value, low, high) { ... }
You could even do it with a single function:
function isBetween(value, low, high, ignoreCase) {
if (isNaN(value)) {
if (ignoreCase) {
value = (value != null ? (new String(value)).toLowerCase() : '');
low = (low != null ? (new String(low)).toLowerCase() : '\xff');
high = (high != null ? (new String(high)).toLowerCase() : '');
}
return (low <= value && value <= high);
} else {
return (+low <= +value && +value <= +high);
}
}
All my code assumes when it's not a Number the "betweenness" can be tested
lexicographically (as Strings). Note also when I manipulate low and high,
if low is null, I convert it to '\xff'. This should make it larger than
any other possible string (you could also do the same thing with unicode
with '\uffff'). The reason should be obvious, if "low" is null or
undefined, value could end up between it and "high". Probably not what you
intended.
In fact, you might want to simply return false if any of the arguments are
undefined or null. It really depends on what contract you want the
function to honor.
--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ -
http://jibbering.com/faq