Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
quailcreek
- Posts: 746
- Joined: Sun Feb 04, 2007 11:01 pm
Post
by quailcreek » Thu Mar 31, 2016 4:00 pm
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
Tom
MacBook Pro OS Mojave 10.14
-
Thierry
- VIP Livecode Opensource Backer

- Posts: 875
- Joined: Wed Nov 22, 2006 3:42 pm
Post
by Thierry » Thu Mar 31, 2016 4:13 pm
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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
-
quailcreek
- Posts: 746
- Joined: Sun Feb 04, 2007 11:01 pm
Post
by quailcreek » Thu Mar 31, 2016 4:29 pm
Thanks, Thierry.
Yep. I realize that's where the problem is but I don't know how to fix it.
Tom
MacBook Pro OS Mojave 10.14
-
dunbarx
- VIP Livecode Opensource Backer

- Posts: 10305
- Joined: Wed May 06, 2009 2:28 pm
Post
by dunbarx » Thu Mar 31, 2016 4:31 pm
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
-
quailcreek
- Posts: 746
- Joined: Sun Feb 04, 2007 11:01 pm
Post
by quailcreek » Thu Mar 31, 2016 5:02 pm
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.
Tom
MacBook Pro OS Mojave 10.14
-
Thierry
- VIP Livecode Opensource Backer

- Posts: 875
- Joined: Wed Nov 22, 2006 3:42 pm
Post
by Thierry » Thu Mar 31, 2016 6:30 pm
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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
-
quailcreek
- Posts: 746
- Joined: Sun Feb 04, 2007 11:01 pm
Post
by quailcreek » Thu Mar 31, 2016 6:36 pm
It works perfectly.

Thanks!
Tom
MacBook Pro OS Mojave 10.14
-
Thierry
- VIP Livecode Opensource Backer

- Posts: 875
- Joined: Wed Nov 22, 2006 3:42 pm
Post
by Thierry » Thu Mar 31, 2016 8:42 pm
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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
-
quailcreek
- Posts: 746
- Joined: Sun Feb 04, 2007 11:01 pm
Post
by quailcreek » Fri Apr 01, 2016 7:19 pm
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?
Tom
MacBook Pro OS Mojave 10.14
-
dunbarx
- VIP Livecode Opensource Backer

- Posts: 10305
- Joined: Wed May 06, 2009 2:28 pm
Post
by dunbarx » Fri Apr 01, 2016 10:08 pm
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
-
quailcreek
- Posts: 746
- Joined: Sun Feb 04, 2007 11:01 pm
Post
by quailcreek » Fri Apr 01, 2016 10:13 pm
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
Tom
MacBook Pro OS Mojave 10.14
-
quailcreek
- Posts: 746
- Joined: Sun Feb 04, 2007 11:01 pm
Post
by quailcreek » Fri Apr 01, 2016 10:20 pm
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.

Tom
MacBook Pro OS Mojave 10.14
-
msellke
- Posts: 22
- Joined: Mon Oct 21, 2013 2:57 am
Post
by msellke » Fri Apr 01, 2016 11:14 pm
Why not try adding the same validation methods to a close field or exit field handler
-
dunbarx
- VIP Livecode Opensource Backer

- Posts: 10305
- Joined: Wed May 06, 2009 2:28 pm
Post
by dunbarx » Sat Apr 02, 2016 1:37 am
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
-
quailcreek
- Posts: 746
- Joined: Sun Feb 04, 2007 11:01 pm
Post
by quailcreek » Sat Apr 02, 2016 1:55 am
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???
Tom
MacBook Pro OS Mojave 10.14