Only number and one point after first number allowed?

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: Klaus, FourthWorld, heatherlaine, kevinmiller

AlessioForconi
Posts: 65
Joined: Sun Feb 15, 2015 2:51 pm

Only number and one point after first number allowed?

Hello,

wanting to accept only numbers and the decimal point I can do so

Code: Select all

``````on keyDown pKey
if pKey is not a number and pKey is not "." then
beep
else
pass keyDown
end if
end keyDown``````
but if instead I wanted to allow only one point and only after the first number how could I do?

Example: 1.2 or 12.3 or 12.34 or 0.12

Thank you

jmburnod
VIP Livecode Opensource Backer
Posts: 2293
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Only number and one point after first number allowed?

Hi,
If I understand you correctly this should work

Code: Select all

``````on keyDown pKey
put ("." is in fld 1) into tExistPoint
put (pKey is  a number ) into tKeyIsNum
put (pKey = ".") into tKeyIsPoint
if tExistPoint and tKeyIsPoint  then
beep
exit keyDown
end if
if tKeyIsNum  or  tKeyIsPoint then
put pKey after fld 1
else
beep
end if
end keyDown``````
Best regards
Jean-Marc

AlessioForconi
Posts: 65
Joined: Sun Feb 15, 2015 2:51 pm

Re: Only number and one point after first number allowed?

Thanks jmburnod,

your code works fine.

I'm really trying to do something a little different.

I would like the item to be accepted only after the first number has been entered.

1.2 or 0.1 but not .1

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

Re: Only number and one point after first number allowed?

Then you should just have to add a check to see if the field is empty or not before accepting the "."
I didn't test this, but something along the lines of adding this line -

Code: Select all

``````on keyDown pKey
put ("." is in fld 1) into tExistPoint
put (pKey is  a number ) into tKeyIsNum
put (pKey = ".") into tKeyIsPoint
if tExistPoint and tKeyIsPoint  then
// if the field is empty, beep and exit
if field 1 is empty then
beep
exit keyDown
end if
end if
if tKeyIsNum  or  tKeyIsPoint then
put pKey after fld 1
else
beep
end if
end keyDown
``````

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

Re: Only number and one point after first number allowed?

What Bogs has mentioned, though not explicitly stated, is that you have to have some information about the target of the data entry in order to validate whether or not the key entry should be allowed.

Your original handler, wholly a keydown handler, cannot know this, and therefore cannot determine whether to pass itself or not.

This is easily worked out, but is there a "field 1" that Bogs had to include? Anyway, whatever that target is, you have to work "inside" that entity (could be a variable) , not from the "source" of data, that is, the keyDown handler.

Craig Newman

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

Re: Only number and one point after first number allowed?

dunbarx wrote:
Mon Nov 05, 2018 5:32 pm
What Bogs has mentioned, though not explicitly stated, is that you have to have some information about the target of the data entry in order to validate whether or not the key entry should be allowed.
Yes, thank you for catching the omission on my part
dunbarx wrote:
Mon Nov 05, 2018 5:32 pm
This is easily worked out, but is there a "field 1" that Bogs had to include?
Erm, I just expanded the code Jean-Marc provided, I can't take credit for that. The question is valid, though.

Alessio did not give any information about how many fields were on his card, and the code provided as an example was generic, so it should be said that if you have more than one field, 'field 1' may not refer to the field in question, and should be replaced with that fields actual name.

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

Re: Only number and one point after first number allowed?

Bogs.

Baby.

I was only expanding on something I thought might be of interest.

And I just reread Jean-Marc'spost, and see that he did indeed introduce that "fld 1" thingie.

Keep the faith.

Craig

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

Re: Only number and one point after first number allowed?

dunbarx wrote:
Tue Nov 06, 2018 2:55 am
I was only expanding on something I thought might be of interest.
I agree it was a needed expansion, I'm just embarassed I didn't expand it myself

I'm just glad you were looking and caught it.

jmburnod
VIP Livecode Opensource Backer
Posts: 2293
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Only number and one point after first number allowed?

keep the faith

and always doubt

jean-Marc (Old Baby)