checking input

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
ethanCodes
Posts: 46
Joined: Sun Feb 14, 2016 9:08 am

checking input

Post by ethanCodes » Sat Feb 20, 2016 2:29 am

How would I check the user input to make sure it is a number and not a letter or something else? I used if else statements to check to make sure the input is within the desired range, but I'm unsure of how to check for letters.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: checking input

Post by dunbarx » Sat Feb 20, 2016 3:22 am

Hi,

If you mean user entry into a field, put this into the field script

Code: Select all

on keyDown tKey
   if tKey is in "0123456789." then pass keyDown 
end keyDown
Now this is deceptively simple looking, but contains within it several key foundational tenets of LC. What do you think these might be?

Craig Newman

ethanCodes
Posts: 46
Joined: Sun Feb 14, 2016 9:08 am

Re: checking input

Post by ethanCodes » Sat Feb 20, 2016 4:50 am

I have no idea. I know so little about LC that I wouldn't even know what to guess. I don't understand how this language doesn't have data types. It seems like this would limit the language so much.

ethanCodes
Posts: 46
Joined: Sun Feb 14, 2016 9:08 am

Re: checking input

Post by ethanCodes » Sat Feb 20, 2016 4:52 am

also, why do people prefix variables with letters? and how do you decide which letter to prefix. eg your example, tkey

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: checking input

Post by Simon » Sat Feb 20, 2016 4:54 am

Here is Richard's excellent post on naming conventions
http://fourthworld.com/embassy/articles ... style.html

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: checking input

Post by FourthWorld » Sat Feb 20, 2016 5:57 am

ethanCodes wrote:I don't understand how this language doesn't have data types. It seems like this would limit the language so much.
The data types are there; what's absent is the tedium of having to manually declare and coerce them.

When we write:

put field 1 + field 2 into field 3

...LiveCode is smart enough to see that we're doing arithmetic so it automatically coerces the display strings from the field structures to numbers, and when we put the result into the third field it knows the field structure needs a string so it converts it for us.

Handy stuff, all taken care of for us so we can focus on crafting the user experience and leave the tedious bit counting to machines
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: checking input

Post by dunbarx » Sat Feb 20, 2016 7:25 am

Ethan.

What Richard said.

Another way to look at it is that LC types its variables based purely on context. If you try to:

Code: Select all

add 3 to horse
you will get a runTime error if horse does not contain a number. But you will not get a compileTime error, since the context has not yet been determined. I am not conversant with strongly typed languages, but would such a statement, or its syntactic equal, get even that far?

You have no idea, as Richard alluded to, how liberating this is. Maybe you will shortly. The question you must ask yourself is simple: can LC accomplish what you need? I guarantee it offers sufficient power and range.

Craig Newman
Last edited by dunbarx on Sun Feb 21, 2016 4:10 am, edited 1 time in total.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: checking input

Post by jacque » Sat Feb 20, 2016 5:15 pm

ethanCodes wrote:How would I check the user input to make sure it is a number
Due to LiveCode's English-like syntax you answered your own question:

Code: Select all

if it is a number then...  
In some cases it's better to iterate the integers like Craig did, but in this case you can probably just use the built-in method.

See "is a" in the dictionary, there are many types you can check for.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

ethanCodes
Posts: 46
Joined: Sun Feb 14, 2016 9:08 am

Re: checking input

Post by ethanCodes » Sun Feb 21, 2016 1:46 am

Thank you for all the replies. You all helped me understand more of the different parts of LC. I'm getting a better understanding of it.

Post Reply