isnumberic check

E

eggie5

How would I check if a string is a number? e.g:

'asdf2' =false
'34' =true
'asf' =false
'0' =true
 
E

Evertjan.

rf wrote on 28 aug 2007 in comp.lang.javascript:
is_numeric($string)

Wrong language, that is NOT javascript.

Try:

var n = '34';
if (!isNaN(n)) alert(n + ' is numeric');
 
R

rf

Evertjan. said:
rf wrote on 28 aug 2007 in comp.lang.javascript:
Wrong language, that is NOT javascript.

Oops :-(

What is even worse is that I actually started typing in isNaN when I
thought: Hey, this is the PHP group.

Some days it's not even worth turning the computer on.
 
R

Richard Maher

Hi,
How would I check if a string is a number? e.g:

If this is "user input" that we're talking about then I would suggest using
what that French guy Stephane (whatever happened to him? I miss him.)
suggested to me, and that is trap the erroneous keystrokes before they're
echoed. Eg: -

if ((target == "yourNumericField") &&
(keyCode < 48 || keyCode > 57))
return false;
else
return true;

This would be called from an "onkeypress" event.

Oh well, up to you.

Cheers Richard Maher
 
R

RobG

Richard said:
Hi,


If this is "user input" that we're talking about then I would suggest using
what that French guy Stephane (whatever happened to him? I miss him.)
suggested to me, and that is trap the erroneous keystrokes before they're
echoed. Eg: -

if ((target == "yourNumericField") &&
(keyCode < 48 || keyCode > 57))
return false;
else
return true;

That must be the worst way to go about it. It has many usability
issues, including preventing decimal numbers, scientific notation,
signs, and so on.

Validate onsubmit and again at the server, read:


<URL: http://www.merlyn.demon.co.uk/js-valid.htm#VNP >



<FAQENTRY>

The link in 3.2 to "Manipulating times, dates and the lastModified date
and time in javascript" is broken:

<QUOTE>

Page Not Found (404)

The merlyn page requested was unavailable.
Possible reasons include :-

Due to the greatly reduced bandwidth now allowed by Demon, the page has
been disabled.
You selected a bad or out-of-date link.
You entered the URL wrongly.
The page is renamed, moved or removed.
If the link was bad, please do not inform its owner.

Try the Wayback Machine, or in some cases a ZIP file; or ask me.

J R Stockton, ? 2007-08-22

</QUOTE>
</FAQENTRY>
 
R

Richard Maher

Hi Rob,
That must be the worst way to go about it. It has many usability
issues, including preventing decimal numbers, scientific notation,
signs, and so on.

Maybe you're right, maybe Eggie did want scientific notation and was
calculating pie to infinity with a reverse Polish calculator. Maybe I could
have a lot to say about the absence of scaled integers in most of these crap
languages and the RobGs of this world spending their days trying to
represent 1.9875E05 dollars worth of carrots in floating point variables.
Just maybe your life long quest is to continue the IBM3270 emulation that
pervades the web today. "All hail the SUBMIT key" is something to aspire
to.

Either way, who gives a toss?

All I did was offer Egmont an extensible alternative for what he was trying
to do. If you choose to censor the material available to him in his fact
gathering then I'm obliged to float the possibility that your prejudice and
myopia have you once again talking out of your arse.

OTY.

Regards Richard Maher
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
oglegroups.com>, Tue, 28 Aug 2007 04:22:51, "(e-mail address removed)"
How would I check if a string is a number? e.g:

'asdf2' =false
'34' =true
'asf' =false
'0' =true

That's not as generally a useful thing to do as is often initially
supposed, but isFinite() seems to do what you actually ask, except that
in javascript "Infinity" and "-Infinity" are good Numbers.

All of your examples are numbers in base 36; only one is a number in
base 2.

The real question is usually whether the string is a valid and
appropriate representation of a number of satisfactory size.

One might order 12 eggs; but there is no need to allow an order of -12
eggs or 0.012e3 eggs or 0xc eggs.

If the string represents an order quantity, then probably one wants to
accept only non-negative integers of not too many digits, but with
leading and trailing whitespace allowed. Testing with a RegExp
/^\s*\d{1,n}\s*$/ should do, with a suitable number n. If the value
range is not bounded by a number like 999, then it's easier to do that
test arithmetically after conversion to Number.
 
R

RobG

Hi Rob,


Maybe you're right, maybe Eggie did want scientific notation and was
calculating pie to infinity with a reverse Polish calculator.

The requirement was to check if a string is a number, attempting to
prevent keyboard entry of characters other than 0 to 9 is a bad way to
achieve that.


[...]
Either way, who gives a toss?

The OP, and anyone who discovers this thread looking for something
like "check string is number".

If you present a solution that you know has limitations, best to spell
them out rather than leave it for others to discover by trial and
error. It will often be their users who will need to submit to the
trial and endure the errors.

All I did was offer Egmont an extensible alternative for what he was trying
to do.

Extensible? By adding more character codes I guess. But simply
restricting the characters that can be entered does not ensure a
number will result. If you allow decimal points for decimal numbers,
then 1.2.3 is likely not considered a "number" by most but will be
allowed by your solution.

Your "solution" also prevents the use of backspace or delete to remove
incorrect entries - more keycodes to add. And maybe the user wants to
use the cursor keys too, hey more keycodes. Maybe they also like to
use tab to move to the next field... how many keycodes do you think
you will end up adding just to allow integers and floats?

And at the end of if all, users can completely bypass your scheme by
using copy and paste.

If you choose to censor the material available to him in his fact
gathering

Censor? I don't have the power to do that, and wouldn't if I did.

then I'm obliged to float the possibility that your prejudice and
myopia have you once again talking out of your arse.

Try to get beyond grade 6. Perhaps you'd like to explain your
solution for the usability issues raised above?
 
P

Petyr David

How would I check if a string is a number? e.g:

'asdf2' =false
'34' =true
'asf' =false
'0' =true

This works for me:

var dayo=document.form11.daysago;
var myRegex = /^\d{1,4}$/;
if (dayo.value.match(myRegex) {
return true;
}
 
G

Gregor Kofler

Petyr David meinte:
This works for me:

var dayo=document.form11.daysago;
var myRegex = /^\d{1,4}$/;
if (dayo.value.match(myRegex) {
return true;
}

That allows numbers from 0 to 9999. What about -1, 500000, 3.1415, etc.?

Gregor
 
R

Richard Maher

Hi Rob,
The requirement was to check if a string is a number, attempting to
prevent keyboard entry of characters other than 0 to 9 is a bad way to
achieve that.

I prefer to leave it to the OP to a) define his requirements and b) decide
which solution best satisfies him.
The OP, and anyone who discovers this thread looking for something
like "check string is number".

Posterity? I'm with ya now.
Extensible? By adding more character codes I guess. But simply
restricting the characters that can be entered does not ensure a
number will result. If you allow decimal points for decimal numbers,
then 1.2.3 is likely not considered a "number" by most but will be
allowed by your solution.

Imagine having to count the number of dots in a string? How awful! Yes, it's
called "programming" Rob, and not very difficult code at that. Are any of
these problems that you throw up meant to be show-stoppers? Is there some
mystery to the functionality in isNaN() that you feel can never be
reproduced in the outside world?

If it's the re-inventing-the-wheel that bothers you then would it not be
possible to trap the keycode and append it to the current value (from
CharCode()?) and then use the omnipotent isNan(newString) to decide whetehr
to return true or false?

Look, I'd personally much prefer to see style="text-transform:
edit-mask(##,##9.99)" but, in the absence of that, I don't think it would
take the OP (or anyone) too long to come up with a isValidNumber(),
isValidInteger, isValidEmail(), isValidPhoneNumber() etc, etc.

But I'm certainly not trying to impose my views on everyone. If RobG thinks
field-level validation is the work of the devil then by all means stick with
the submit key! Who cares that the error was some 20 fields ago and has had
a ripple effect throughout the other fields on the screen, you make that
User go back and do it all again. Yes, block-mode validation and IBM 3270s,
oh happy days! Maybe give them all punch-cards?

As far as Java Applets and predictive text functionality goes, RobG and Pol
Pot say "Do it at the server! The old way much better.", and if God wanted
us to support Ajax then he would've made us all Dutch!
And at the end of if all, users can completely bypass your scheme by
using copy and paste.

Rob, this is all about assisting users in having a pleasurable, responsive,
and productive navigation across the screen. If they're hell-bent on getting
around something then the server will pick it up. But, as I said above,
edit-mask text transformation would be nice.
Try to get beyond grade 6. Perhaps you'd like to explain your
solution for the usability issues raised above?

Although in hindsight my comment was inappropriate, it was just a more
overly familiar form of "I think you're very much mistaken" rather than
abusive. As for usability issues, I leave it up to the OP/readers to decide
if there is any merit in the approach that I've outlined.

When it comes to "usability" and the web in general, I've found that people
are more than willing to put up with anything including that context-devoid
and connectionless pile-of-pooh that is HTTP with its dodgy cookies, session
Id's, expiration dates and Session Hijacking. But hey, each to their own,
it's a broad church.

Regards Richard Maher

RobG said:
Hi Rob,


Maybe you're right, maybe Eggie did want scientific notation and was
calculating pie to infinity with a reverse Polish calculator.

The requirement was to check if a string is a number, attempting to
prevent keyboard entry of characters other than 0 to 9 is a bad way to
achieve that.


[...]
Either way, who gives a toss?

The OP, and anyone who discovers this thread looking for something
like "check string is number".

If you present a solution that you know has limitations, best to spell
them out rather than leave it for others to discover by trial and
error. It will often be their users who will need to submit to the
trial and endure the errors.

All I did was offer Egmont an extensible alternative for what he was trying
to do.

Extensible? By adding more character codes I guess. But simply
restricting the characters that can be entered does not ensure a
number will result. If you allow decimal points for decimal numbers,
then 1.2.3 is likely not considered a "number" by most but will be
allowed by your solution.

Your "solution" also prevents the use of backspace or delete to remove
incorrect entries - more keycodes to add. And maybe the user wants to
use the cursor keys too, hey more keycodes. Maybe they also like to
use tab to move to the next field... how many keycodes do you think
you will end up adding just to allow integers and floats?

And at the end of if all, users can completely bypass your scheme by
using copy and paste.

If you choose to censor the material available to him in his fact
gathering

Censor? I don't have the power to do that, and wouldn't if I did.

then I'm obliged to float the possibility that your prejudice and
myopia have you once again talking out of your arse.

Try to get beyond grade 6. Perhaps you'd like to explain your
solution for the usability issues raised above?
 

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
474,156
Messages
2,570,878
Members
47,413
Latest member
KeiraLight

Latest Threads

Top