Checking an inputted field for numeric

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Tom
Posts: 11
Joined: Wed Feb 13, 2008 10:25 pm

Checking an inputted field for numeric

Post by Tom » Tue Mar 04, 2008 2:51 am

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

EzCoder
Posts: 10
Joined: Sun Sep 30, 2007 12:05 am

Post by EzCoder » Tue Mar 04, 2008 3:31 am

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:

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
HTH,
EZ

(I was reading through the doc's yesterday and noticed it :wink: )

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Tue Mar 04, 2008 11:28 am

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

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Thu Mar 06, 2008 7:05 am

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:

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
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.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Contact:

Post by trevordevore » Thu Mar 06, 2008 3:01 pm

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.
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

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Thu Mar 06, 2008 8:47 pm

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:

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
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.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

Post Reply