458,142 Members | 1,718 Online Need help? Post your question and get tips & solutions from a community of 458,142 IT Pros & Developers. It's quick & easy.

# compare alphanumeric problem

 P: n/a 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. 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!!

Jul 23 '05 #1

4 Replies

 P: n/a 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. It is still not clear what you think is acceptable: Does it have to start with "N"? Followed by a number from 11800 to 12800? What does "dynamic" mean in this context? Mick Jul 23 '05 #2

 P: n/a 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.
Value 1:
Value 2:
Enter a value between Value 1 and Value 2: 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 comp.lang.javascript FAQ - http://jibbering.com/faq Jul 23 '05 #3 