How to test for a number

Rob Cozens rcozens at pon.net
Wed Mar 16 11:35:07 EST 2005


Hi Jonathan,

>I have a field where I restrict it to only enter numbers...
>
>But I had to include extra script to allow the character to be a "." or a 
>"-", in case the number included a decimal point or minus sign:
>
>On KeyDown theKey
>      If (isNumber(theKey) = true) or theKey = "." or theKey = "-" then 
> pass keyDown
>End KeyDown

And you do check to see there is only one "." and/or "-" in the character 
string, and that "-", if any, is char 1 or -1, right?

The following function is included in Serendipity Library 
<http://wecode.org/serendipity/>.

It checks for your issues and Klaus'; based on the contents of the stack's 
numberEditMask custom property, which designates decimal & thousands 
separator characters, currency symbol (to three characters}, and currency 
symbol placement (leading/trailing).

function validNumber theString,allowCurrency -- 08 Jan 04:RCC
   put (allowCurrency is true) into allowCurrency--defaults to false
   get the numberEditMask of this stack
   if it is empty then
     modal "Number Edit Mask"
     get the numberEditMask of this stack
     if it is empty then return sdbMessage(sdbNumberFormatError)
   end if
   put (char 1 of it is "<") into prefixCurrancy
   put char 2 to -1 of line 1 of it into currencyString
   put char 1 of line 2 of it into theDecimalSeparator
   put char 2 of line 2 of it into theThousandsSeparator
   put stripBlanks(theString,false) into strippedString
   put the length of strippedString into stringLength
   put 0 into periodPosition
   put 0 into characterNumber
   put empty into commaList
   put false into foundMinus
   repeat for each char theCharacter in theString
     add 1 to characterNumber
     if theCharacter is not in "0123456789-" and theCharacter is not 
theDecimalSeparator and theCharacter is not theThousandsSeparator and 
(theCharacter is not in currencyString or not allowCurrency) then return false
     if theCharacter is "-" then
       if foundMinus or (characterNumber <> 1 and (characterNumber <> 
(length(currencyString)+1) or not allowCurrency or not prefixCurrency)) 
then return false
       put true into foundMinus
     end if
     if theCharacter is in currencyString then
       if not allowCurrency then return false
       if prefixCurrency then
         if characterNumber > length(currencyString) or 
offset(currencyString,theString) <> 1 then
           return false
         end if
       else if offset(currencyString,theString) <> 
(length(theString)-length(currencyString)+1) then return false
     end if
     if theCharacter is theThousandsSeparator then
       if periodPosition is not 0 or characterNumber is 1 then return false
       else put characterNumbe&return after commaList
     end if
     if theCharacter is theDecimalSeparator then put characterNumber into 
periodPosition
   end repeat
   if commaList is empty then return true
   if periodPosition is 0 then put stringLength+1 into periodPosition
   repeat with x = number of lines of commaList down to 1
     get (line x of commaList+0)
     if periodPosition - it <> 4 then return false
     subtract 4 from periodPosition
   end repeat
   return true
end validNumber

The Library includes other handlers to strip the thousand separators and 
force the decimal separator to "." for numeric manipulation and convert 
calculation results for localized display.

Rob Cozens, CCW
Serendipity Software Co.

"First they ignore you.
Then they laugh at you.
Then they fight you.
Then you win."

-- Gandhi



More information about the use-livecode mailing list