Page 2 of 2
Re: Limiting a field to 2 decimal places
Posted: Sat Apr 02, 2016 3:09 am
by dunbarx
Oh.
You mean find a way to make the thing not work. Why din't you say so?
I did not account for being able to insert a decimal in front of more than two digits, only that you could not "build" such a thing.
Back to the drawing board. But can you play with it as well?
Craig
Re: Limiting a field to 2 decimal places
Posted: Sat Apr 02, 2016 3:20 am
by dunbarx
So here is what I put in, just one line to brute-force test each annoying instance of where the thing can fail.
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 pKey = "." and the length of me - charNumber > 2 then exit to top --STUCK THIS LINE IN TO CATCH THOSE PESKY INSERTIONS
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
Craig
Re: Limiting a field to 2 decimal places
Posted: Sat Apr 02, 2016 3:42 am
by quailcreek
I think we have a winner! That did it. I even set the charNumber > 0 to force the insertion point to be at the end of the string and she worked.

Re: Limiting a field to 2 decimal places
Posted: Mon Apr 04, 2016 8:03 pm
by TerryL
Great fun. The closeField suggestion could handle integer only and re-entering decimal point by applying restrictive formatting with numberFormat, which also auto-rounds. Would this help? Terry
Code: Select all
on keyDown X --allow only numbers and 1 decimal point, formatted for 2 decimal places
if X is a number or X = "." and "." is not in me then pass keyDown
end keyDown
on closeField
set the numberFormat to "0.00" --2 decimal places
put me * 1 into me --apply format, auto-rounds
end closeField
on returnInField
closeField
end returnInField
on enterInField
closeField
end enterInField
Re: Limiting a field to 2 decimal places
Posted: Mon Apr 04, 2016 8:31 pm
by quailcreek
Thanks, Terry.
Another very good option with a little different approach.
Re: Limiting a field to 2 decimal places
Posted: Tue Apr 05, 2016 1:27 pm
by AxWald
Hi,
I did something similar some years ago. My experience was that "the common User™" becomes confused when what it types isn't displayed - its mental capabilities are often less than desired, and instead of realizing that it types something wrong it may cry for the IT department to replace its keyboard ...
So I ended in a solution similar to this:
Code: Select all
function CheckMyInput tGarbage
-- some code that checks tGarbage, and, if it's valid:
return chrToNum(1) & tGarbage
-- if it's not valid, it makes a guess at what it could mean:
return charToNum(2) & format("%xyz23#!", tGarbage)
-- if it is empty or makes no sense at all:
return empty
end function
on closeField
get CheckMyInput(me)
if char 1 of it is chrToNum(1) then -- we're OK
play "Fanfare"
else if char 1 of it is chrToNum(2) then
delete char 1 of it
put it into me
answer information "Your input was rather strange." & CR & \
"We expect a certain format here, it's [explanation]." & CR & CR & \
"I took a guess, check if it is OK!"
play "Boing"
else
put [defaultValue] into me
answer error "Your input made no sense at all." & CR & \
"We expect a certain format here, it's [explanation]." & CR & CR & \
"It should look like the current entry, try again!"
play "Burp"
end if
end closeField
Even if "the common User™" isn't usually the brightest of the human race, learning by repeating surprisingly often works ;-)
Have fun!
PS: You may omit the "play Fanfare". But keep the other sounds, "the common User™" loves such, and it will greatly help with the learning experience! Just be sure to record them at a low volume ...
Re: Limiting a field to 2 decimal places
Posted: Fri Apr 08, 2016 12:13 pm
by [-hh]
Yet another method --> put into script of input field.
Code: Select all
-- Limits input to "money"-format. Selects last 'wrong' input
on textChanged
lock screen; lock messages
put the selectedChunk into sc
if me is not a number then
beep; put word 4 of sc into word 2 of sc --> optional
else
set numberformat to "0.00"
put round(me,2) into me --> 'financial' rounding
end if
do "select sc"
unlock screen; unlock messages
end textChanged