Page 1 of 1
checking input
Posted: Sat Feb 20, 2016 2:29 am
by ethanCodes
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.
Re: checking input
Posted: Sat Feb 20, 2016 3:22 am
by dunbarx
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
Re: checking input
Posted: Sat Feb 20, 2016 4:50 am
by ethanCodes
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.
Re: checking input
Posted: Sat Feb 20, 2016 4:52 am
by ethanCodes
also, why do people prefix variables with letters? and how do you decide which letter to prefix. eg your example, tkey
Re: checking input
Posted: Sat Feb 20, 2016 4:54 am
by Simon
Here is Richard's excellent post on naming conventions
http://fourthworld.com/embassy/articles ... style.html
Simon
Re: checking input
Posted: Sat Feb 20, 2016 5:57 am
by FourthWorld
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
Re: checking input
Posted: Sat Feb 20, 2016 7:25 am
by dunbarx
Ethan.
What Richard said.
Another way to look at it is that LC types its variables based purely on context. If you try to:
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
Re: checking input
Posted: Sat Feb 20, 2016 5:15 pm
by jacque
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:
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.
Re: checking input
Posted: Sun Feb 21, 2016 1:46 am
by ethanCodes
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.