Page 1 of 2
Limiting a field to 2 decimal places
Posted: Thu Mar 31, 2016 4:00 pm
by quailcreek
Hi,
I'm trying to make it so the user can only enter numbers and allow there to be only 2 decimal places entered. I have that part working. The problem is that after entering the 2 numbers after the decimal, I can't enter or edit any numbers in front of the decimal. Any help would be appreciated.
Code: Select all
on keyDown pKey
put the text of me into theValue
set the itemDel to "."
if theValue contains "." then
put the number of chars of item 2 of theValue into tCentsCount
if tCentsCount > 1 then
exit keyDown
else
pass keyDown
end if
else if pKey is a number then
pass keyDown
else
if pKey is "." and"." is not in the text of me then
pass keyDown
else
exit keyDown
else
if pKey is a number then
pass keyDown
end if
end if
end if
end keyDown
Re: Limiting a field to 2 decimal places
Posted: Thu Mar 31, 2016 4:13 pm
by Thierry
quailcreek wrote:
Code: Select all
on keyDown pKey
put the text of me into theValue
set the itemDel to "."
if theValue contains "." then
put the number of chars of item 2 of theValue into tCentsCount
if tCentsCount > 1 then
exit keyDown -- <-- as soon as you have 2 or more decimals you'll always exit!
else
pass keyDown
end if
end keyDown
Re: Limiting a field to 2 decimal places
Posted: Thu Mar 31, 2016 4:29 pm
by quailcreek
Thanks, Thierry.
Yep. I realize that's where the problem is but I don't know how to fix it.
Re: Limiting a field to 2 decimal places
Posted: Thu Mar 31, 2016 4:31 pm
by dunbarx
Hi.
So much fun. In the field script:
Code: Select all
on keyUp pKey
set the itemDel to "."
if pKey is in "0123456789." and the length of item 2 of me < 3 then pass keyUp
end keyUp
Craig Newman
Re: Limiting a field to 2 decimal places
Posted: Thu Mar 31, 2016 5:02 pm
by quailcreek
Doh! Thanks Craig. From 23 to 2 lines of code. However, I still have the problem that once there are 2 decimal places I can't enter or edit the chars in item 1 of the text. But this is still a big improvement.
Re: Limiting a field to 2 decimal places
Posted: Thu Mar 31, 2016 6:30 pm
by Thierry
quailcreek wrote:Thanks, Thierry.
Yep. I realize that's where the problem is but I don't know how to fix it.
Ok, try this one and type any chars, even extra dots....
and let us know.
Code: Select all
on keyDown pKey
local V
set the itemDel to "."
put the text of me into V
if pKey is not in "0123456789." then exit keyDown
if pKey is "." and "." is in V then exit keyDown
if item 2 of V is not empty and \
(word 2 of the selectedChunk of me) > offset( ".", V) and \
the length of item 2 of V > 1 then exit keyDown
pass keyDown
end keyDown
Kind regards,
Thierry
Re: Limiting a field to 2 decimal places
Posted: Thu Mar 31, 2016 6:36 pm
by quailcreek
It works perfectly.

Thanks!
Re: Limiting a field to 2 decimal places
Posted: Thu Mar 31, 2016 8:42 pm
by Thierry
quailcreek wrote:It works perfectly.

Thanks!
Mmm, in fact it doesn't work perfectly.
I can see at least 2 use cases which breaks the logic.
One is:
type 1st a list of numbers, e.g: 123456789
put the cursor in the middle, and then type a dot.
Second is left as an exercise for the reader
Best,
Thierry
Re: Limiting a field to 2 decimal places
Posted: Fri Apr 01, 2016 7:19 pm
by quailcreek
Ok, so how do we trap the enter key from adding another line?
I've looked around and I see the first problem you pointed out even on some websites, etc where the user needs to enter a dollar value. I also need to be able to do this same thing on some of my iOS apps in the native input control. Any ideas?
Re: Limiting a field to 2 decimal places
Posted: Fri Apr 01, 2016 10:08 pm
by dunbarx
Hi.
Nothing like exhaustive testing, eh? Easy to get 95%, not so easy to get 100. Put this in the field script.
Code: Select all
on keyDown pKey
set the itemDel to "."
put offset(".",me) into decChar
put word 4 of the selectedChunk of me into charNumber
if charnumber < decChar then put "Left" into whichSide else put "Right" into whichSide
if pKey = "." and "." is in me then exit keyDown
if pKey is in "0123456789." then
switch
case whichSide = "Left" and pKey <> "."
pass keyDown
break
case whichSide = "Right" and "." is not in item 2 of me
if the length of item 2 of me < 2 then pass keyDown
break
end switch
end if
end keyDown
on enterInField
end enterInField
on returnInField
end returnInField
I left it wordy, so you can follow. Nothing wrong with wordy by the way.
Craig
Re: Limiting a field to 2 decimal places
Posted: Fri Apr 01, 2016 10:13 pm
by quailcreek
Cool Craig. I'll give yours a try. Wordy is good... I like wordy!
Been working a little on he mobile version. This allows only one ".". Still working on the 2 decimal place part.
Code: Select all
on inputTextChanged
put mobileControlTarget() into tTargetEnd
put mobileControlGet (tTargetEnd,"text") into tTypedStuff
put the last char of tTypedStuff into theLastChar
if tTypedStuff contains ".." and theLastChar contains "." then
delete last char of tTypedStuff
mobileControlSet tTargetEnd,"text", tTypedStuff
end if
end inputTextChange
Re: Limiting a field to 2 decimal places
Posted: Fri Apr 01, 2016 10:20 pm
by quailcreek
Great Craig. This does Work almost Perfectly. For such a short piece of code its got a little bit of everything. Working with offset, text chunks, switch statement... very cool! Thanks a lot!
But alas, the problem of entering several number and then cursoring over and typing a "." is still there. This allows more than 2 decimal places.

Re: Limiting a field to 2 decimal places
Posted: Fri Apr 01, 2016 11:14 pm
by msellke
Why not try adding the same validation methods to a close field or exit field handler
Re: Limiting a field to 2 decimal places
Posted: Sat Apr 02, 2016 1:37 am
by dunbarx
But alas, the problem of entering several number and then cursoring over and typing a "." is still there. This allows more than 2 decimal places.
Not for me. Type a few numbers. Type a decimal. It takes, and then allows only two numbers thereafter. No new decimals allowed.
Backspace to lose the decimal, or just start over with a string of digits, then place the cursor anywhere in the string. Either new numbers may be entered, or exactly one decimal point. I cannot break it, but then, I can only break things that I do not want broken.
Do let me know if you find a way to have this not be 100%.
Craig
Re: Limiting a field to 2 decimal places
Posted: Sat Apr 02, 2016 1:55 am
by quailcreek
I've tried using LC 6, 7 and 8. Same result. Type in some numbers, say 5. Then place the cursor after the first number and type a "." I get a number with 4 decimal places. One field on a new stack with your script in the fld. What am I missing???