Ensuring numeric input

Ken Ray kray at sonsothunder.com
Thu Oct 27 00:15:34 EDT 2005


On 10/26/05 10:58 PM, "Michael J. Lew" <michaell at unimelb.edu.au> wrote:

> Thanks for the suggestions, but unfortunately it doesn't seem to be
> anywhere near that easy. A user can put the insertion point within a
> number and so I need to check before the character is entered whether
> the value will be a number after the new character is added at the
> insertion point. The new character may be at the start of a numeric
> string, in the middle or at the end. I can't find an easy function
> that gives the insertion point relative to the selectedLine. Most of
> the complexity of my script is in deciding where the insertion will
> be made in the line of interest.
> 
> Still lacking a simple solution...

Try this one (script of the field):

on keydown whichKey
    if whichKey is among the chars of "01234567890.-" then
        put me into tTemp
        put word 2 of the selectedChunk into sChar
        put word 4 of the selectedChunk into eChar
        if eChar > sChar then -- some text is selected
            delete char sChar to eChar of tTemp
        end if
        put whichKey after char eChar of tTemp
        if isNumber(tTemp) then pass keyDown
    end if
end keyDown

Basically it checks to see if the end product would end up with a numeric
value, and if so, it will allow the keystroke to happen.

BTW: There was no reason to trap for backspace/delete as there isn't a way
AFAIK to remove some or part of a number and have the end result *not* be a
number.

HTH,

Ken Ray
Sons of Thunder Software
Web site: http://www.sonsothunder.com/
Email: kray at sonsothunder.com




More information about the use-livecode mailing list