473,586 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2771
rf

<eg****@gmail.c omwrote in message
news:11******** **************@ l22g2000prc.goo glegroups.com.. .
How would I check if a string is a number? e.g:

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

--
Richard.
Aug 28 '07 #2
rf wrote on 28 aug 2007 in comp.lang.javas cript:
>
<eg****@gmail.c omwrote in message
news:11******** **************@ l22g2000prc.goo glegroups.com.. .
>How would I check if a string is a number? e.g:

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

is_numeric($str ing)
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.net wrote in message
news:Xn******** ************@19 4.109.133.242.. .
rf wrote on 28 aug 2007 in comp.lang.javas cript:
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 == "yourNumericFie ld") &&
(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.c omwrote in message
news:11******** **************@ l22g2000prc.goo glegroups.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 == "yourNumericFie ld") &&
(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 "Manipulati ng 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.ne t.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 == "yourNumericFie ld") &&
(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 "Manipulati ng 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.co m 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...@hotsp amnotmail.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 "programmin g" 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(), isValidPhoneNum ber() 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.ne t.auwrote in message
news:11******** **************@ x40g2000prg.goo glegroups.com.. .
On Aug 28, 11:07 pm, "Richard Maher" <maher...@hotsp amnotmail.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
4169
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 equal 59001.31. Change each side of the equation by a hundreth or two and it checks. Change it a bit more, and it won't. _What_ is going on...
13
3550
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 will be deleted. But I'm running into difficulty... Right now the NAME property of each check box is the primary key of the corresponding...
7
29888
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 *** Don't just participate in USENET...get rewarded for it!
2
27761
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 form is a command button. When the command button is clicked, I need the VBA scripting to determine which Check Box has been selected, and then...
1
4255
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 require checking a check box to tell which category something will pertain to. Well after each record is entered i want the check to remain with that...
2
3583
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 check/uncheck it. Also, what event do I use to check it for a value? Any articles out there? -- Chris Davoli
16
5648
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
11645
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 Gun_Number (assigned gun number by operations)
1
2404
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. I want to display these results in check boxes. This is my code. But it doesn't work <tr><td><input value="1" type="checkbox" id="1" name="check"...
0
7908
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8199
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8212
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5710
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5389
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3835
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1175
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.