Re: Numeric valication
- From: "Trevor L." <Trevor_L.@Canberra>
- Date: Wed, 19 Apr 2006 11:07:58 +1000
My reply at end
Dr John Stockton wrote:
JRS: In article <uvz6RRqYGHA.4060@xxxxxxxxxxxxxxxxxxxx>, dated Tue,
18 Apr 2006 14:55:28 remote, seen in
news:microsoft.public.scripting.jscrip t, Trevor L.
<Trevor_L.?@Canberra.?> posted :
This is a topic I have just started to learn, and it should work
quite well here if you get the syntax correct
I thought this might work
if (/^\-?[0-9]|\.?/.test(vartotest)) == true
Before learning RegExps, one should learn Booleans. Part '== true' is
not necessary (and your final parenthesis should be at the end).
Moreover, the RegExp is wrong; it accepts any string EITHER
containing a minus followed by a digit OR containing a dot which
might not be there - in other words, AFAICS, *anything* apart from an
undefined vartotest (vartotest defined, but with value undefined,
passes OK).
Thanks, John
I can see the error in the if statement
It should be
if (condition)
OR
if (condition == true)
It wasn't so much that I didn't understand booleans. I knew that "== true"
was redundant - I put it there for clarity.
My error was that I misplaced the ')'
I will plead having an old day as my excuse. My JS knowldege is not
extensive (about 15 months worth, all picked up form the net) but as an
ex-programmer, I should have known/remebered enough to place the ')'
correctly ;-))
And thanks for pointing out the error in the regexp
So I guesss I shoud have written:
/^\-?[0-9]\.?/
BUT AFAICS, this accepts any string containing a leading minus which might
not be there, followed by a digit AND followed by a dot which might not be
there
However, the dot is in the wrong position.
So to further my knowledge of regexps, I will study your examples further.
Is this the correct interpratation of your OK1 = /^-?\d+(\.\d+)?$/.test(S)
any string which commences with a minus which might not be there, followed
by a digit 1 or more times, followed by a single dot and a digit 1 or more
times both of which might not be there but which must be at the end of the
string.
I take it that "compatible with IUPAP/SUNAMCO" means that this is some
standard definition of a number.
--
Cheers,
Trevor L.
Website: http://tandcl.homemail.com.auhanks,
.
- References:
- Numeric valication
- From: abcd
- Re: Numeric valication
- From: Trevor L.
- Re: Numeric valication
- From: Dr John Stockton
- Numeric valication
- Prev by Date: Re: Numeric valication
- Next by Date: Re: Numeric valication
- Previous by thread: Re: Numeric valication
- Next by thread: Re: Numeric valication
- Index(es):
Relevant Pages
|