Verifying Numeric Input

Rob Cozens rcozens at pon.net
Sun Aug 17 18:58:38 EDT 2008


Ken, et al:

>BTW, Rob - just curious, but when would you see allowing multiple "-"s or
>multiple currency symbols?

I wouldn't...which is why I fault keyDown "numeric input editor 
handlers" that allow the decimal separator, minus sign, or currency 
symbol regardless of their frequency (or, in the case of the latter 
two, position) in the input text.

FWIW, here's my handler from Serendipity Library.  It validates input 
as compliant with the user's number formatting preferences, stored as 
a custom stack property, "numberEditMask":

function validNumber theString,allowCurrency -- 08 Jan 04:RCC
   put (allowCurrency is true) into allowCurrency  -- empty argument 
defaults to false
   get the numberEditMask of this stack
   if it is empty then
     modal "Number Edit Mask" -- sets the stack's numberEditMask
     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 --currency 
symbol may be multiple characters
   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 -- removes 
leading spaces, if any
   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


Rob Cozens CCW
Serendipity Software Company

"And I, which was two fooles, do so grow three;
  Who are a little wise, the best fooles bee."

  from "The Triple Foole" by John Donne (1572-1631) 


More information about the use-livecode mailing list