By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,246 Members | 1,515 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,246 IT Pros & Developers. It's quick & easy.

isnumberic check

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

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

Aug 28 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
rf

<eg****@gmail.comwrote in message
news:11**********************@l22g2000prc.googlegr oups.com...
How would I check if a string is a number? e.g:

'asdf2' =false
'34' =true
'asf' =false
'0' =true
is_numeric($string)

--
Richard.
Aug 28 '07 #2

P: n/a
rf wrote on 28 aug 2007 in comp.lang.javascript:
>
<eg****@gmail.comwrote in message
news:11**********************@l22g2000prc.googlegr oups.com...
>How would I check if a string is a number? e.g:

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

is_numeric($string)
Wrong language, that is NOT javascript.

Try:

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

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 28 '07 #3

P: n/a
rf

"Evertjan." <ex**************@interxnl.netwrote in message
news:Xn********************@194.109.133.242...
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.

--
Richard.
Aug 28 '07 #4

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

<eg****@gmail.comwrote in message
news:11**********************@l22g2000prc.googlegr oups.com...
How would I check if a string is a number? e.g:

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

Aug 28 '07 #5

P: n/a
Richard Maher wrote:
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;
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>
--
Rob
"We shall not cease from exploration, and the end of all our
exploring will be to arrive where we started and know the
place for the first time." -- T. S. Eliot
Aug 28 '07 #6

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

"RobG" <rg***@iinet.net.auwrote in message
news:46***********************@per-qv1-newsreader-01.iinet.net.au...
Richard Maher wrote:
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;

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>
--
Rob
"We shall not cease from exploration, and the end of all our
exploring will be to arrive where we started and know the
place for the first time." -- T. S. Eliot

Aug 28 '07 #7

P: n/a
eg****@gmail.com wrote:
How would I check if a string is a number? e.g:

'asdf2' =false
'34' =true
'asf' =false
'0' =true
isFinite('34') === true

http://javascript.crockford.com/
Aug 28 '07 #8

P: n/a
On Aug 28, 11:07 pm, "Richard Maher" <maher...@hotspamnotmail.com>
wrote:
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.
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?
--
Rob

Aug 28 '07 #9

P: n/a
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" <rg***@iinet.net.auwrote in message
news:11**********************@x40g2000prg.googlegr oups.com...
On Aug 28, 11:07 pm, "Richard Maher" <maher...@hotspamnotmail.com>
wrote:
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.

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?
--
Rob

Sep 1 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.