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

tlottrike
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 11
Joined: Mon Oct 31, 2011 3:46 pm
Location: Edinburgh, Scotland
Contact:

When is a Number a Number?

Post by tlottrike » 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
IMac MacOSX 10.13
Livecode 9.0.0 Build 15103

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

Re: When is a Number a Number?

Post by dunbarx » Thu Dec 20, 2018 2:43 pm

The issue is that LC is evaluating each char in each keypress to see if it is a number. So though it is true that "27.5" is a number , it is also true that "." is not.

Do this instead:

Code: Select all

if theKey is in "0123456789." then...
Craig Newman

tlottrike
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 11
Joined: Mon Oct 31, 2011 3:46 pm
Location: Edinburgh, Scotland
Contact:

Re: When is a Number a Number?

Post by tlottrike » Thu Dec 20, 2018 3:47 pm

Thanks again that sorted it.
IMac MacOSX 10.13
Livecode 9.0.0 Build 15103

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: When is a Number a Number?

Post by [-hh] » Thu Dec 20, 2018 5:43 pm

Hi all.

Is "1.1.1" a number? No.
Is "+1" a number? Yes.
Is "-1" a number? Yes.

You could try the following in your field's script.
This works also when pasting text into the field.

Code: Select all

on textchanged
  put 0 into k
  repeat for each line L in me
    add 1 to k
    if L is not a number then
      select line k of me
      exit repeat
    end if
  end repeat
end textchanged
shiftLock happens

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

Re: When is a Number a Number?

Post by dunbarx » Thu Dec 20, 2018 5:54 pm

Hermann.

Your handler will somewhat guard against non-numeric entry. It is a little too tough on the user, though, eh? If you type a series of digits and then a "K", it deletes the whole line on the next keypress. And if you type "55X" and manually select the next line, it keeps that string intact.

I suggest to pre-validate the keypress itself instead of evaluating the line after the entry.

Craig

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: When is a Number a Number?

Post by [-hh] » Thu Dec 20, 2018 6:14 pm

Craig.
Right, of course the user should set what to do if non-numbers were inserted.
But catching keypresses doesn't handle paste, not "1.." what is not even a version number and also not negative numbers.
shiftLock happens

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

Re: When is a Number a Number?

Post by dunbarx » Thu Dec 20, 2018 6:59 pm

Hermann.

The devil is always in the details. We just need to deal with them (him?), eh?:

Code: Select all

on keyDown tkey
   put word 2 of the selectedLine into tLine
   if tKey = "-" and "-" is in line tLine of me then exit to top
   if tKey = "+" and "+" is in line tLine of me then exit to top
   if tKey = "." and "." is in line tLine of me then exit to top
   if tkey is in "0123456789-+." then pass keyDown
end keyDown
That sort of thing.

Craig

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: When is a Number a Number?

Post by [-hh] » Thu Dec 20, 2018 7:11 pm

Craig.
Perhaps this could go into a prototype of a "numeric text field"?
Before we get too much sympathy for the devil!?
Ah, what's puzzling you
Is the nature of my game
shiftLock happens

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

Re: When is a Number a Number?

Post by dunbarx » Thu Dec 20, 2018 7:49 pm

@tlottrike

Can you test this and see what else can go wrong? It is devilishly hard to catch every possible case.

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 3:29 pm

What about adding 0 and then looking at what comes out?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9662
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 5:47 pm

@Mikey.

Code: Select all

 if tkey is in "0123456789-+." and tKey + 0 is a number then pass keyDown
So see if the handler breaks? Not sure how to manage that. :wink:

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7235
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: When is a Number a Number?

Post by jacque » Fri Dec 21, 2018 6:35 pm

Another method:

Code: Select all

on keyDown thekey
   If me & thekey is not a number then answer information "Must be a number"
   else pass keyDown  
end keyDown
This assumes the entry is after the existing text though and will fail if the selection is in the middle somewhere. An alternative would be to use keyup and remove the last entry if the text isn't a number.
Last edited by jacque on Fri Dec 21, 2018 6:39 pm, edited 1 time in total.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9662
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 6:37 pm

Jacque.

But this would prevent any of the "+-." chars from being loaded the first time.

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7235
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: When is a Number a Number?

Post by jacque » Fri Dec 21, 2018 6:40 pm

dunbarx wrote:
Fri Dec 21, 2018 6:37 pm
Jacque.

But this would prevent any of the "+-." chars from being loaded the first time.

Craig
Yeah, probably. It depends on the context. I often use your method too.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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 7:11 pm

I meant instead of the other code.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”