compare alphanumeric problem

M

Matt

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!!

<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>
 
M

Mick White

Matt said:
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
 
G

Grant Wagner

Matt said:
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.
 
A

Antonie C Malan Snr

Hi Matt,

Let me get this straight - you want to make sure a number entered is
between two numeric values?

First, I would get them to enter just a number. So use isNaN(theValue)
to make sure they entered a clean numeric value. The A333 value entered
would of course fail this test.

I suppose you have a onsubmit="return checkFields(this);" to check your
form fields before accepting them. In that case do the following:

if(isNaN(form.theField.value) || form.theField.value < 11800
||form.theField.value > 12800){
form.theField.focus();
alert("Please enter a number only between 11800 and 12800");
return false;
}

If I understand your prolem correctly, this should do it.

Chris
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Sun, 19 Sep 2004 12:58:52, seen in Antonie C
Malan Snr <[email protected]> posted :

Responses should go after trimmed quotes; please see FAQ & comply.
Let me get this straight - you want to make sure a number entered is
between two numeric values?

Manifestly not, since the examples start with "N".
First, I would get them to enter just a number. So use isNaN(theValue)
to make sure they entered a clean numeric value. The A333 value entered
would of course fail this test.

That is a weak test; in practical cases, the number can be far more
constrained; for example, to start with 1..9, and to have no more than a
certain number of digits after. A RegExp test does that well; see my
js-valid.htm. But it is possible that the OP's inputs are better
handled as strings.

I suppose you have a onsubmit="return checkFields(this);" to check your
form fields before accepting them. In that case do the following:

if(isNaN(form.theField.value) || form.theField.value < 11800
||form.theField.value > 12800){
form.theField.focus();
alert("Please enter a number only between 11800 and 12800");
return false;
}

Better to get the value first, so that it is only sought once.

var X = +form.theField.value // number or NaN
if (!( X<=12880 && X>=11800 ) ) { // complain ...

Note that this takes advantage of NaN comparing FALSE always. It does
allow input of 1.2e4 and 0x3222, for example.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top