473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

compare alphanumeric problem

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>
Jul 23 '05 #1
4 2596
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
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

Jul 23 '05 #3
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

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.

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>


Jul 23 '05 #4
JRS: In article <41***********************@news.optusnet.com.au> , dated
Sun, 19 Sep 2004 12:58:52, seen in news:comp.lang.javascript, Antonie C
Malan Snr <ma*******@optusnet.com.au> 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.

Matt wrote:
I posted this problem before but no reply.
...

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Nobody | last post by:
Heres the deal... I have an application where I have a list (as in a Windows list control, but thats not important) displayed to the user. I sort this list based on the list controls sort function...
3
by: Marcel | last post by:
Hello, I'm trying te maken an update SQL statement. I Use the function Between And. The problem is that the values I use by between are alphanumeric. Now the function between doesn't work....
3
by: Bill | last post by:
I just ran into a situation where string data from a mainframe contained a couple of non-alphanumeric characters (hex CC and C8). I was parsing a field that occurred after these unexpected...
7
by: Fernando Rodríguez | last post by:
Hi, How can I know if a string only has alfanumeric chars? Thanks
1
by: White Spirit | last post by:
I'm trying to use getchar() to read alphanumeric data as follows:- char input; /* Take a string of input and remove all spaces therein */ int j = 0; while ((input = getchar()) != '\n') { if...
8
by: dohyohdohyoh | last post by:
I have a programming question to generate an ordered list of alphanumeric strings of length 4. two alphabets rest numberst, etc. EG 0000-9999 then A000-Z999 then AA00 to ZZ99 then AAA0 - ZZZ9...
2
by: colinawright | last post by:
Sorry to intrude on your tech environment but I don't know where to look. I am using a new computer (Windows XP professional) that will not print many of the non alphanumeric characters (such as...
3
by: Paul73 | last post by:
Hi everyone, I'm hoping someone can help me. I've created a windows app that inserts data into a sql database. The data comes from textboxes. This is a simple app that will only be used by...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.