Checking an inputted field for numeric
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Checking an inputted field for numeric
I need to make sure an entered field is numeric... here's current code:
on mouseDown
local x
ask question "How many total miles for this ride? (nn.nn)"
put it into x
repeat while (x < 0 or x > 999.9)
ask warning "Enter value between .1 and 999.9"
end repeat
Set the Label of me to x
end mouseDown
The issue, for example, is that entering 2g2 is accepted.
Tom
on mouseDown
local x
ask question "How many total miles for this ride? (nn.nn)"
put it into x
repeat while (x < 0 or x > 999.9)
ask warning "Enter value between .1 and 999.9"
end repeat
Set the Label of me to x
end mouseDown
The issue, for example, is that entering 2g2 is accepted.
Tom
Hello Tom,
I believe this is what you are looking for from the documentation:
How do I prevent entering certain characters in a field?
You use a keyDown or rawKeyDown handler to prevent certain characters from being typed into a field.
The handler intercepts each keystroke, and the character is entered into the field only if you pass the keyDown or rawKeyDown message.
Example given:
HTH,
EZ
(I was reading through the doc's yesterday and noticed it
)
I believe this is what you are looking for from the documentation:
How do I prevent entering certain characters in a field?
You use a keyDown or rawKeyDown handler to prevent certain characters from being typed into a field.
The handler intercepts each keystroke, and the character is entered into the field only if you pass the keyDown or rawKeyDown message.
Example given:
Code: Select all
keyDown keyName
on keyDown theKey -- block typing anything but digits
if theKey is not a number then beep
else pass keyDown
end keyDown
EZ
(I was reading through the doc's yesterday and noticed it

Tom,
Code: Select all
on mouseDown
local x
ask question "How many total miles for this ride? (nn.nn)"
if it is a number then
put it into x
repeat while (x < 0 or x > 999.9)
ask warning "Enter value between .1 and 999.9"
end repeat
Set the Label of me to x
end if
end mouseDown
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
-
- VIP Livecode Opensource Backer
- Posts: 977
- Joined: Sat Apr 08, 2006 7:47 am
- Contact:
Actually, just trapping the 'keyDown' event is not enough, as this message is not sent when people paste text or drag-and-drop text into the field.
Here's a quick-and-dirty script which should handle most cases:
The downside of the above script is that the new content shows up briefly before the text is restored. That's because I've postponed the check until after the original event has been handled by the engine.
Another approach is to check inline by prechecking what the new text will be like, on the basis of the current selection and the event that is about to transpire. But that is elft as an exercise to the reader
Jan Schenkel.
Here's a quick-and-dirty script which should handle most cases:
Code: Select all
on keyDown pKey
put the text of me into tOldText
put the selectedChunk into tOldSelection
send "CheckContent tOldText, tOldSelection" to me in 0 milliseconds
pass keyDown
end keyDown
on pasteKey
put the text of me into tOldText
put the selectedChunk into tOldSelection
send "CheckContent tOldText, tOldSelection" to me in 0 milliseconds
pass pasteKey
end pasteKey
on dragDrop
put the text of me into tOldText
put the selectedChunk into tOldSelection
send "CheckContent tOldText, tOldSelection" to me in 0 milliseconds
pass dragDrop
end dragDrop
on CheckContent pOldText, pOldSelection
if the text of me is not a number then
beep
set the text of me to pOldText
do ("select" && pOldSelection)
end if
end CheckContent
Another approach is to check inline by prechecking what the new text will be like, on the basis of the current selection and the event that is about to transpire. But that is elft as an exercise to the reader

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com
www.quartam.com
-
- VIP Livecode Opensource Backer
- Posts: 1005
- Joined: Sat Apr 08, 2006 3:06 pm
- Contact:
Jan,
I just tested pasteKey in a standalone with a menubar and the message is not sent to fields if your menubar has command/control + v assigned to Edit-Paste. If you don't have your own menubar then the pasteKey message is sent. I tested in 2.8 and 2.9 so it looks like pasteKey can't be relied upon.
The other issue would be if you have a contextual menu that gives standard options such as cut/copy/paste/select all. In this case selecting "copy" would send no message to the field.
So perhaps one addition to the code you provided would be a custom message called "TextChanged" that did the same thing as pasteKey. The developer could send this message to the field in the Edit->Paste code of the Edit menu as well as from the Paste code in the contextual menu.
I just tested pasteKey in a standalone with a menubar and the message is not sent to fields if your menubar has command/control + v assigned to Edit-Paste. If you don't have your own menubar then the pasteKey message is sent. I tested in 2.8 and 2.9 so it looks like pasteKey can't be relied upon.
The other issue would be if you have a contextual menu that gives standard options such as cut/copy/paste/select all. In this case selecting "copy" would send no message to the field.
So perhaps one addition to the code you provided would be a custom message called "TextChanged" that did the same thing as pasteKey. The developer could send this message to the field in the Edit->Paste code of the Edit menu as well as from the Paste code in the contextual menu.
Trevor DeVore
ScreenSteps - https://www.screensteps.com
LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder
ScreenSteps - https://www.screensteps.com
LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder
-
- VIP Livecode Opensource Backer
- Posts: 977
- Joined: Sat Apr 08, 2006 7:47 am
- Contact:
Well, I did say it was quick-and-dirty, didn't I? 
My regular script does indeed handle everything through a custom handler before anything is modified in the field. However, I couldn't just extract that bit, as it is a large frontscript that combines a lot of features.
But let's see if we can make good on the earlier attempt:
The above may need some fiddling for the dragSource, but it seemed to work good in my tests, and a custom "Paste" command can tap into the system quite easily.
Hope this helped,
Jan Schenkel.

My regular script does indeed handle everything through a custom handler before anything is modified in the field. However, I couldn't just extract that bit, as it is a large frontscript that combines a lot of features.
But let's see if we can make good on the earlier attempt:
Code: Select all
on keyDown pKey
if pKey is in "0123456789." and ActionAllowed("keydown", pkey)
then pass keyDown
else beep
end keyDown
on pasteKey
if ActionAllowed("pastekey")
then pass pasteKey
else beep
end pasteKey
on dragDrop
put the text of me into tOldText
put the selectedChunk into tSelectedChunk
put the dragSource into tDragSource
if tDragSource is not empty
then put the htmlText of tDragSource into tSourceHtmlText
send "CheckDragDropAction tOldText, tSelectedChunk, tDragSource, tSourceHtmltext" to me in 0 milliseconds
pass dragDrop
end dragDrop
on CheckDragDropAction pOldText, pSelectedChunk, pDragSource, pSourceHtmlText
if the text of me is not a number then
beep
set the text of me to pOldText
if pDragSource is not empty then
set the htmlText of pDragSource to pSourceHtmlText
end if
do ("select" && pSelectedChunk)
end if
end CheckDragDropAction
function ActionAllowed pAction, pParameter
put the text of me into tText
put the selectedChunk of me into tSelectedChunk
put word 1 to 4 of tSelectedChunk into tSelectedChunk
switch pAction
case "keyDown"
do ("put pParameter into" && tSelectedChunk && "of tText")
break
case "pasteKey"
if the clipboard is not "text" then return false
do ("put the clipboardData[" & quote & "text" & quote & "] into" && tSelectedChunk && "of tText")
break
end switch
return (tText is a number)
end ActionAllowed
Hope this helped,
Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com
www.quartam.com