E
eggie5
How would I check if a string is a number? e.g:
'asdf2' =false
'34' =true
'asf' =false
'0' =true
'asdf2' =false
'34' =true
'asf' =false
'0' =true
How would I check if a string is a number? e.g:
'asdf2' =false
'34' =true
'asf' =false
'0' =true
is_numeric($string)
Evertjan. said:rf wrote on 28 aug 2007 in comp.lang.javascript:
Wrong language, that is NOT javascript.
How would I check if a string is a number? e.g:
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.
How would I check if a string is a number? e.g:
'asdf2' =false
'34' =true
'asf' =false
'0' =true
How would I check if a string is a number? e.g:
'asdf2' =false
'34' =true
'asf' =false
'0' =true
Hi Rob,
Maybe you're right, maybe Eggie did want scientific notation and was
calculating pie to infinity with a reverse Polish calculator.
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.
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;
}
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.
The OP, and anyone who discovers this thread looking for something
like "check string is number".
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.
And at the end of if all, users can completely bypass your scheme by
using copy and paste.
Try to get beyond grade 6. Perhaps you'd like to explain your
solution for the usability issues raised above?
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?
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.