Page 1 of 2
When is a Number a Number?
Posted: Thu Dec 20, 2018 2:03 pm
by tlottrike
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
Re: When is a Number a Number?
Posted: Thu Dec 20, 2018 2:43 pm
by dunbarx
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
Re: When is a Number a Number?
Posted: Thu Dec 20, 2018 3:47 pm
by tlottrike
Thanks again that sorted it.
Re: When is a Number a Number?
Posted: Thu Dec 20, 2018 5:43 pm
by [-hh]
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
Re: When is a Number a Number?
Posted: Thu Dec 20, 2018 5:54 pm
by dunbarx
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
Re: When is a Number a Number?
Posted: Thu Dec 20, 2018 6:14 pm
by [-hh]
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.
Re: When is a Number a Number?
Posted: Thu Dec 20, 2018 6:59 pm
by dunbarx
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
Re: When is a Number a Number?
Posted: Thu Dec 20, 2018 7:11 pm
by [-hh]
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
Re: When is a Number a Number?
Posted: Thu Dec 20, 2018 7:49 pm
by dunbarx
@tlottrike
Can you test this and see what else can go wrong? It is devilishly hard to catch every possible case.
Craig
Re: When is a Number a Number?
Posted: Fri Dec 21, 2018 3:29 pm
by Mikey
What about adding 0 and then looking at what comes out?
Re: When is a Number a Number?
Posted: Fri Dec 21, 2018 5:47 pm
by dunbarx
@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.
Craig
Re: When is a Number a Number?
Posted: Fri Dec 21, 2018 6:35 pm
by jacque
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.
Re: When is a Number a Number?
Posted: Fri Dec 21, 2018 6:37 pm
by dunbarx
Jacque.
But this would prevent any of the "+-." chars from being loaded the first time.
Craig
Re: When is a Number a Number?
Posted: Fri Dec 21, 2018 6:40 pm
by jacque
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.
Re: When is a Number a Number?
Posted: Fri Dec 21, 2018 7:11 pm
by Mikey
I meant instead of the other code.