When is a Number a Number?

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: When is a Number a Number?

Post by dunbarx » Fri Dec 21, 2018 9:43 pm

@Mikey I assumed you meant instead. Can you give a short example?

But in virtually all cases, I believe that validating the keyPress well before anything gets placed into the field is the way to go.

Craig

Mikey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 755
Joined: Fri Jun 27, 2008 9:00 pm

Re: When is a Number a Number?

Post by Mikey » Fri Dec 21, 2018 10:47 pm

I would agree that validating the key press is a good thing to do. It just becomes harder if you want to allow more flexibility with the field.
What I was suggesting just off the cuff without testing it was letting the user have at the field, and validate the contents after they're done with it, for example, by adding 0 to the contents and checking that if the normal number checks don't work.

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: When is a Number a Number?

Post by MaxV » Tue Jan 22, 2019 2:20 pm

tlottrike wrote:
Thu Dec 20, 2018 2:03 pm
I want a Field to only have numbers entered into ie. no text.

So I looked at the code in the Lessons Book on page 127 and put this together for my Field script

Code: Select all

on keyDown thekey
   If thekey is not a number then answer information "Must be a number"
   else pass keyDown 
PUT me into nareasqm 
end keyDown
However when I test it, it will not let me enter a decimal point eg. 27.5 yet the Dictionary says a Number can have decimal place. So is this a glitch with LC or is my code doing something to make it think it's an Integer
Don't EVER use keydown for this purpose, but use closeField, because user can mistype and correct it. Closefield is activated when the user finished to write, the content changed and he exit the field.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: When is a Number a Number?

Post by dunbarx » Tue Jan 22, 2019 3:41 pm

Max.
Don't EVER ...
What you propose so strongly is, to me, more a matter of the user experience than the best way to solve the problem. In other words, a matter of style.

In yet other words, is it better to deny the entry, char by char, of illegal keypresses, or is it better to deny the totality of the complete contents of the field?

I go with the first. :wink:

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: When is a Number a Number?

Post by dunbarx » Tue Jan 22, 2019 6:12 pm

Given that I like to validate keyPresses instead of the contents of the field, I think this is pretty good:

Code: Select all

 on keyDown tKey
   if (tkey is in ".+--" and me is empty) or (tKey is in "0123456789." and me & tKey is a number)
   then pass keyDown
end keyDown
Untested, but this either allows a valid starting char in an empty field, or tests the appending of the most recent (validated) char to the contents of the field, and if that string is a number, it allows the char to be added.

It seems to work when one tries to add a bogus char to the interior of the field as well. The test appending a bogus char also works as well in the middle. Just lagniappe.

Craig

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: When is a Number a Number?

Post by MaxV » Wed Jan 23, 2019 1:47 pm

dunbarx wrote:
Tue Jan 22, 2019 3:41 pm
Max.
Don't EVER ...
What you propose so strongly is, to me, more a matter of the user experience than the best way to solve the problem. In other words, a matter of style.

In yet other words, is it better to deny the entry, char by char, of illegal keypresses, or is it better to deny the totality of the complete contents of the field?

I go with the first. :wink:

Craig
Well, I used EVER to stress the point. Deny char by char create a lot of problems in many, many context. If user don't finish to type what he wants, you can't know what it really meant. This case is for numbers, but letters, words, any possible situation is much better closeField, because there are many unimaginable situation that you can't predict the error until the user finish to type.
For example in some asian languages, user type different keys and some combination change the typed chars to other (like, you press "USD" and after typing "D", the "USD" become "$").
Or some typing softwares for quite blind people. If you block user to finish to type, you risk that he will never be able to type.
Denying char by char you risk a situation as the famous error message "Mouse not found, click here with mouse to proceed." :lol:
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: When is a Number a Number?

Post by dunbarx » Wed Jan 23, 2019 3:20 pm

Max.

Ah. You make a point if a multi-char string is processed as a single "entry". I suppose that if there were a complete list of such combinations, they could be addressed on a case by case basis.

Just goes to show that nothing is as simple as is seems. But I live in the United States, where everyone is as simple as they seem. :D

Craig

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: When is a Number a Number?

Post by bogs » Wed Jan 23, 2019 5:43 pm

Image <- PDS
{Pretty Dang Simple}
Image

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”