Ensuring numeric input

Raymond E. Griffith rgriffit at ctc.net
Thu Oct 27 07:28:55 EDT 2005


> 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...

It depends on what you are looking for.

If you are looking for integer values, you can use "x is an integer",
otherwise you can use "x is a number" to check the result.

How about something like this? You can store a last legitimate value as a
custom property. If the user inputs something that doesn't fit, you can
simply replace it with the last correct value. This is not tested, but you
shouldn't have too many problems with it.

I didn't test the key pressed. I tested the result. Good luck.

On keyup whichkey
  put the selectedchunk into tselchunk
  if value(me) is a number then
    put value(me) into the storedvalue of me
    put value(me) into me
  else
    put the storedvalue of me into me
  end if
  select tselchunk
On keyup

Regards,

Raymond E. Griffith

> 
> At 8:56 PM -0500 26/10/05, use-revolution-request at lists.runrev.com wrote:
>> the isNumber of value
>> isNumber(value)
>> 
>> isNumber(8) -- returns true
>> isNumber(1+5) -- returns true
>> isNumber(foo) -- returns false
>> 
>> Dennis
> 
> Thanks, but the 'isnumber' function is the same as the 'is a number'
> that I used in my script.
> 
> 
> At 8:56 PM -0500 26/10/05, use-revolution-request at lists.runrev.com wrote:
>> You can also add zero to it; if it is a number then the result will be
>> empty.
>> Paul Looney
> 
> Thanks, but that is not functionally different from simply using 'is
> a number'. Or is it?
> 
> At 10:34 AM +1000 27/10/05, Michael J. Lew wrote:
>> How can I prevent users from being able to make non-number values in
>> a field? Simply preventing non-numeric keys is not enough because I
>> need to prevent things that use characters that are in valid numbers
>> to make non-numbers like 1.2.3 or -1.2-3.
>> 
>> I thought it would be relatively easy, but it might not be. Here is
>> the field script of a surprisingly complicated attempt:
>> 
>> on keydown thekey
>>   if theKey is in "0123456789-." then --may be an allowable input
>>     -- but I still need to check whether the result would be a valid number.
>>     put value(the selectedLine) into thisLine --current number
>>     put length(thisLine) into thisLineLen
>>     put the selectedCHunk into sc --returns character locations from
>> start of field
>>    
>>     --Need to find where the current line starts
>>     put offset(thisLine,me,min(0,word 2 of sc-thislinelen)) into
>> thisLineStart
>>     --The characters to skip bit is an attempt to prevent the offset function
>>     --  from returning a match to an earlier line in the field.
>>     --  It needs the min function to prevent negative values.
>>    
>>     --Now see if the input would make a non-numeric result
>>     --First find where the selection point is in the line
>>     put word 2 of sc +1 - thisLineStart into theSelectionStartChar
>>     put word 4 of sc +1 - thisLineStart into theSelectionEndChar
>>     --I don't know why I had to add one to the values...
>>     put thisLineStart & return & theSelectionStartChar && theSelectionEndCHar
>>     --Now test the input in the relevant place
>>     put theKey into char theSelectionStartChar to
>> theSelectionEndChar of thisLine
>>    
>>     --next line to help debugging
>>     --put  thisLine & return & thisLineStart & return &
>> theSelectionStartChar && theSelectionEndChar
>>    
>>     if thisLine & "0" is a number then --should be OK
>>       --Needs the appended zero to allow a line to start with a decimal
>> point.
>>       pass KeyDown
>>     end if
>>   end if
>> end keydown
>> 
>> 
>> It seems to work, but it's an extraordinarily long and winding road
>> to get to a simple end-point. What am I missing?
>> 
>> Thanks,
>> Michael
> 
> 





More information about the use-livecode mailing list