Making a Numbers-Only Field
Mac OS 9 Mac OS X Windows Linux

It took me a bit to construct, and it's a combination of bits and pieces from earlier posts, but here's the "definitive" solution for numeric fields *chuckle*
--|
--| Field script for a numeric field
--|

local sBeforePaste

on keyDown pWhichKey
  put the selectedChunk into tChunk
  put word 2 of tChunk into tStart
  put word 4 of tChunk into tStop
  if tStart > tStop -- no actual selection
    put (char 1 to tStop of me) & pWhichKey into tCheck
    if length(me) > theStart then
      put (char tStart to -1 of me) after tCheck
    end if
  else -- typing would replace the selected text
    put (char 1 to tStart - 1 of me) & pWhichKey into tCheck
    if length(me) > theStop then
      put (char tStop + 1 to -1 of me) after tCheck
    end if
  end if
  if Conv4Calc(tCheck) is a number then pass keyDown
  else beep
end keyDown

--|
--| Reformat upon leaving the field
--|

on closeField
  if "formatField" is not in the pendingMessages then send "formatField" to me in 5 milliseconds
end closeField

on formatField
  put the text of me into tNumber
  put Conv4Disp(tNumber) into me
end formatField

--|
--| Handle pasting of text in this field
--| NOTE : doesn't work when Rev UI is ON
--|

on pasteKey
  put the text of me into sBeforePaste
  if "checkAfterPaste" is not in the pendingMessages then send "checkAfterPaste" to me in 5 milliseconds
  pass pasteKey
end pasteKey

on checkAfterPaste
  put the text of me into tAfterPaste
  if Conv4Calc(tAfterPaste) is not a number then
    beep
    put sBeforePaste into me
  end if
end checkAfterPaste

--|
--| Conversion between calculation and display formats
--|

function Conv4Calc pNumber
  -- strip out the thousand separator (if any)
  put the uThousandSeparator of me into t1000Sep
  if t1000Sep is not empty then replace t1000Sep with "" in pNumber
  
  -- fix the decimal point for MetaCard/RunRev
  put the uDecimalPoint of me into tDecPoint
  if tDecPoint is not empty and tDecPoint <> "." then replace tDecPoint with "." in pNumber
  
  -- should now be a regular number
  return pNumber
end Conv4Calc

function Conv4Disp pNumber
  -- convert to the chosen floating point format
  put the uFormat of me into tFormat
  if tFormat is empty then put "%16.2f" into tFormat
  put format(tFormat, pNumber) into tNumber
  
  -- remove leading spaces
  put 1 into tStart
  repeat while char tStart of tNumber = " "
    add 1 to tStart
  end repeat
  put char tStart to -1 of pNumber into tNumber
  
  -- prepare the display format
  -- thanks to Ken Ray for this RegExp :-)
  local tMinusHold,tMainNumber,tDecimalHold
  get matchText(tNumber,"([-]?)([0-9]*)[\.]?([0-9*)", tMinusHold, tMainNumber, tDecimalHold)
  
  -- tweak tDecimalHold
  put the uDecimalPoint of me into tDecPoint
  if tDecPoint is empty then put "." into tDecPoint
  if tDecimalHold is not empty then put tDecPoint before tDecimalHold
  
  -- now determine how many separators to place
  put the uThousandSeparator of me into t1000Sep
  if t1000Sep is not empty then
    put length(tMainNumber) into tLength
    put (tLength DIV 3) into tSeps
    if (tLength MOD 3) = 0 then subtract 1 from tSeps
    
    -- insert the commas into the integer part
    repeat with i = tSeps down to 1
      put t1000Sep after char - (i * 3 + 1) of tMainNumber
    end repeat
  end if
  
  -- put everything back together
  return tMinusHold & tMainNumber & tDecimalHold
end Conv4Calc

--- End of script
Hope this suits everyone's needs ; all you need to do is fill in the following custom properties: Of course you can always adapt the scripts so that these custom properties are in the card, stack,...

Have fun!

Posted 9/26/2002 by Jan Schenkel to the Use Revolution List