473,396 Members | 1,961 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,396 software developers and data experts.

isnumberic check

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

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

Aug 28 '07 #1
9 2759
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Craig Bailey | last post by:
Someone please explain what alternate universe I fell into this afternoon when PHP started telling me that 2 doesn't equal 2. Not sure about you, but when I run this, it tells me 59001.31 doesn't...
13
by: Adrian Parker | last post by:
I have a PHP generated page which displays X many records. Each record has a checkbox preceding it. The user checks several checkboxes, and hits a delete button. All the corresponding records...
7
by: Tony Johnson | last post by:
Can you make a check box very big? It seems like when you drag it bigger the little check is still the same size. Thank you, *** Sent via Developersdex http://www.developersdex.com ***...
2
by: Travis.Box | last post by:
I have an MS Access userform with 16 Check Boxes. Each of the checkboxes has a different option value, which coincides with the Check Box name (eg. cb01.OptionValue = 1). At the bottom of the...
1
by: scprosportsman | last post by:
Please help guys, i am trying to set up a database here at work and im fairly new to access in terms of writing functions and queries and stuff. I have 2 different places on my design that will...
2
by: Chris Davoli | last post by:
How do you enable a check box in the GridView. I selected Checkbox Field in the Columns of the GridView, and the check box shows up in the Grid view, but it is disabled. How do I enable it so I can...
16
by: Brian Tkatch | last post by:
Is there a way to check the order in which SET INTEGRITY needs to be applied? This would be for a script with a dynamic list of TABLEs. B.
5
by: starke1120 | last post by:
Im creating a check in – check out database for RF guns. I have a table that contains models. ID (primary key) Model A table that contains Gun Details ID (primary key) Model_id...
1
by: ghjk | last post by:
my php page has 7 check boxes. I stored checked values to database and retrive as binary values. This is the result array Array ( => 0 => 1 => 0 => 1 => 0 => 0 => 1 ) 1 means checked....
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.