Numbers or text only in a 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
TodayIsTheDay
Posts: 56
Joined: Sat Jun 20, 2009 2:41 pm

Numbers or text only in a field?

Post by TodayIsTheDay » Mon Oct 12, 2009 12:28 am

Is there a simple way to only let a user enter numbers into a text box? (or letters only for that matter?)

In another language I remember where you could mask or set a field to only accept one or the other. Is there something like that in RunRev?

Mark Smith
Posts: 179
Joined: Sat Apr 08, 2006 11:08 pm
Contact:

Post by Mark Smith » Mon Oct 12, 2009 11:12 am

If you put this in the script of your field:

Code: Select all

on keyDown pKey
   if pKey is in "0123456789." then pass keydown
end keyDown
The user will be restricted to entering only the characters listed.

Of course it gets more complex if you want to avoid a leading decimal point, or multiple decimal points, but you get the idea.

Best,

Mark Smith

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Mon Oct 12, 2009 5:03 pm

You can get more comprehensive with this too. There are various stacks around which you can examine, a handy one that I don't have to look up where to find is in RevOnline (v 3.5+) by George Brackett, search for "Allowed Chars Behavior".

chris9610
Posts: 79
Joined: Fri Mar 20, 2009 4:38 pm

Post by chris9610 » Mon Oct 12, 2009 8:11 pm

Ok boys I am new but here is a script which has evolved into one of my fields.

Code: Select all

on keyDown theKey
   if the length of the selectedText of me > 0 and thekey is a number then pass keyDown 
   else if thekey is "." and offset(".",me) = 0 then pass keydown
   else if theKey is not a number or the length of me >= 5 then focus me
   else pass keyDown
end keyDown
on exitfield
   put format("%05.2f",me) into newvar
   put newvar into me
end exitfield
on closefield
   put format("%05.2f",me) into newvar
   put newvar into me
end closefield
This may not be perfect but it works.
Developing with Windows XP & Revolution 4.5

TodayIsTheDay
Posts: 56
Joined: Sat Jun 20, 2009 2:41 pm

Post by TodayIsTheDay » Mon Oct 12, 2009 10:36 pm

Thanks! That gives me a couple of routes to try!

Post Reply