Formatting Numeric Field

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
warrenk
Posts: 110
Joined: Sun Sep 21, 2008 5:39 am

Formatting Numeric Field

Post by warrenk » Wed Dec 03, 2008 7:44 pm

I created a field where I am only allowing numbers and a decimal place. Is there anyway to prevent more then one decimal place? If I use the code below, I can only allow numerics and a decimal place. The problem is I can't prevent multiple decimal places.

on keyDown theKey
if theKey is a number or theKey = "." and the length of me <10 then
pass keyDown
else
end if
end keyDown

Thanks!
Warren

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

Post by Janschenkel » Wed Dec 03, 2008 8:34 pm

Almost there - just need to put in an extra check.

Code: Select all

on keyDown theKey 
  if theKey is a number or theKey = "." and the length of me <10 then
    if theKey is not "." or "." is not in the text of me then
      pass keyDown 
    end if
  end if 
end keyDown
Bear in mind that this doesn't help when the user pastes or drag-and-drops text into your field. So you'll also have to write handlers for the 'pasteKey' and 'dragDrop' messages.

HTH,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

warrenk
Posts: 110
Joined: Sun Sep 21, 2008 5:39 am

Post by warrenk » Wed Dec 03, 2008 8:41 pm

Awesome!!! Thanks for the help Jan!

Warren

Post Reply