Testing for numerics
Dar Scott
dsc at swcp.com
Sat Sep 30 19:52:44 EDT 2006
On Sep 30, 2006, at 12:56 PM, Francis Nugent Dixon wrote:
> I want to check the contents of a field for numeric,
> so, for a field of four characters, I coded :
>
> matchText(field MyTextField,"[0-9][0-9][0-9][0-9]")
> if it is true then ......
>
> Is this the right way to check for numerics, and if
> so, what have I done wrong ?
Revolution defines matchText as a function. The above script will
look for a command named matchText. (An implied 'get' does sound
like an interesting idea.)
So, first of all, change the above to this:
get matchText(field MyTextField,"[0-9][0-9][0-9][0-9]")
if it is true then ......
This is also OK, if you are content with the readability:
get matchText(field MyTextField,"[0-9][0-9][0-9][0-9]")
if it then ......
Or this:
if matchText(field MyTextField,"[0-9][0-9][0-9][0-9]") then ......
That will match text that contains 4 digits in a row and so will
match "around 1966 or so" or "4/5/1966". Perhaps this does what you
want:
if matchText(field MyTextField,"\A[0-9]{4}\z") then ......
The \A and \z match at the start and end respectively. The {4} means
exactly 4 times.
You might want to consider "is a number". Use it like this:
if field MyTextField is a number then ......
That will match some numerals that you might not expect, such as
"1.3", "1.0E01", "+inf" (on OS X) and "0xF0E".
More information about the use-livecode
mailing list