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.
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
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
! SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
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.
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
! SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
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?
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.
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
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.
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%.
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???