Page 1 of 1

Number Range in Input Field

Posted: Sat Apr 25, 2020 8:33 pm
by velcrobelly
I need to have an input field that accepts numbers only within the range of -20 to 20.

I have part of the code working, but can't figure out the range feature. Any help would be appreciated.

Code: Select all

on KeyDown theKey
   if (the text of me & theKey) is a number then
      pass keyDown
   else if the text of me is empty and theKey is "-" then
      pass keyDown
   end if
end KeyDown

Re: Number Range in Input Field

Posted: Sat Apr 25, 2020 9:41 pm
by Klaus
Hi velcrobelly,

welcome to the forum!

Do this, tested and works. :-)

Code: Select all

on keyDown tKey
   
   ## 1. Filter input!
   if tKey is NOT in "-0123456789" then
      exit keydown
   end if
   
   ## 2. Allow the MINUS sign as first character!
   if me = EMPTY then
      pass keydown
   end if
   ## ME = short for -> the text of me
   
   ## 3. Now check possible new content
   put me & tKey into tNewNumber
   if tNewNumber is a number AND tNewNumber > -21 AND tNewNumber < 21 then
      pass keydown
   end if
end keyDown
Best

Klaus

Personal note:
A little Hello or something would not have hurt for the very first posting.

Re: Number Range in Input Field

Posted: Sat Apr 25, 2020 9:51 pm
by velcrobelly
This works great. Thanks!

Re: Number Range in Input Field

Posted: Sat Apr 25, 2020 10:26 pm
by dunbarx
Just to waste time, since Klaus' post was spot on, I sometimes just like to try to make such things as short as possible.

For no good reason.

Code: Select all

on KeyDown tKey
   if tKey = "-" and "-" is not in me then put "-" into me
   if me & tKey  >= -20 and me & tKey <= 20 then put tkey after me
end KeyDown

on mouseEnter -- not needed, I just like to refresh
   put "" into me
   select the text of me
end mouseEnter
Two lines, Klaus.

But is that the user experience you wanted (for either method)? That is, if you have a "2" in place, the field simply refuses to accept, say, a "5"?

The old fashioned way to do this is to put up an "ask" dialog, and validate the total entry. If it flies, load the value. If it does not, announce that fact, so the user can try again.

Just sayin'

Craig

Re: Number Range in Input Field

Posted: Sun Apr 26, 2020 12:16 pm
by Klaus
Yes, Craig, two lines! :D

I'm sure I could squeeze this into ONE line, but I wanted to make the underlying logics more clear! 8)

Re: Number Range in Input Field

Posted: Sun Apr 26, 2020 3:11 pm
by dunbarx
Klaus.

One line, eh?

I could not make something like this work:

Code: Select all

if tKey = "-" and "-" is not in me then put "-" into me else if me & tKey  >= -20 and me & tKey <= 20 then put tkey after me
You can see why. Without separating the snippet that handles "-" and the one that handles the numbers, the single line above will allow a second "-" to appear (as, say, "-1-").

I would love to see you take a crack at a one liner. :wink:

Craig

Re: Number Range in Input Field

Posted: Sun Apr 26, 2020 3:52 pm
by Klaus
OK, OK, I lied, the important part of my sentence is the second one. :D

Re: Number Range in Input Field

Posted: Sun Apr 26, 2020 5:10 pm
by dunbarx
Klaus.

As I said, I only do this sort of thing to waste time.

Craig

Re: Number Range in Input Field

Posted: Sun Apr 26, 2020 5:48 pm
by velcrobelly
So, then another challenge for either of you...

After the field has content, then I try to edit the field by selecting/highlighting the characters in the field using the mouse, it does not allow me to type in a new number. I have to hit delete or backspace. Is there a way to take that into consideration?

Re: Number Range in Input Field

Posted: Sun Apr 26, 2020 6:03 pm
by dunbarx
I suppose anything can be accomplished in LC with enough fooling around. Perhaps the original handler might only fire if the option key is down, and if up (its natural state?), the field is simply open for editing.

But this wrinkle is just the sort of reason I asked whether an "ask" dialog, requesting the user to enter data, with subsequent validation, is a better approach. The constraints placed on the field in the original implementation are interfering with the "editing" you now want to do in that field.

It is often the case that one has to simply rethink the flow of one's script, and take another tack.

Craig

Re: Number Range in Input Field

Posted: Sun Apr 26, 2020 6:15 pm
by SparkOut
A slider or dial to restrict the options to the limited range of choices is another possibility. It is possible to script for keyboard control with arrow keys etc, for good measure.

Re: Number Range in Input Field

Posted: Sun Apr 26, 2020 6:26 pm
by dunbarx
Sparkout.

Hi.

Do you mean a slider with range -20 to +20, where the user drags the thumb to the desired value and lets that value be loaded into the field? You are a man after my own heart. :wink:

Cute, but a bit cumbersome, no? And we can only hope that one day the range does not go from -3000 to +3000.

The issue is the original (cute) idea about letting a handler manage successive keystrokes into a field (which is after my own heart as well).

It needs to be abandoned if this other stuff is now required.

Craig

Re: Number Range in Input Field

Posted: Mon Apr 27, 2020 4:03 pm
by dunbarx
So, then another challenge for either of you...

After the field has content, then I try to edit the field by selecting/highlighting the characters in the field using the mouse, it does not allow me to type in a new number. I have to hit delete or backspace. Is there a way to take that into consideration?
Hi again.

If you select a char in the field, and then try to add another number, the very nature of the handler prevents this; it thinks you want a three digit entry, which is forbidden. If you delete the second char, and then type a number, assuming it is within range, the field accepts that entry. If delete char 1, move the cursor to the right, and key a number, the field will accept it, assuming as always it is in range.

If you highlight the entire text, again any new keystroke would be seen as trying to enter a three digit number. So the latest kluge would be:

Code: Select all

on KeyDown tKey
   if tKey = "-" and "-" is not in me then put "-" into me
   if me & tKey  >= -20 and me & tKey <= 20 then put tkey after me
   
   if the length of the selection = the length of me then -detects when all chars are selected
      put "" into me
      if tKey = "-" and "-" is not in me then put "-" into me
      if me & tKey  >= -20 and me & tKey <= 20 then put tkey after me
   end if
end KeyDown
Just adorable, no?

Ahem.

Just so you know, there is not much room left in all this to accommodate yet more gadgetry. And I do not think it worthwhile to place the working (duplicated) snippet into its own handler.8)

Craig