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

Post Reply
AlessioForconi
Posts: 90
Joined: Sun Feb 15, 2015 2:51 pm

Only number and one point after first number allowed?

Post by AlessioForconi » Sat Nov 03, 2018 7:16 am

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
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

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

Post by jmburnod » Sat Nov 03, 2018 2:26 pm

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
https://alternatic.ch

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

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

Post by AlessioForconi » Sat Nov 03, 2018 4:56 pm

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: 5435
Joined: Sat Feb 25, 2017 10:45 pm

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

Post by bogs » Sat Nov 03, 2018 5:43 pm

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
Image

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

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

Post by dunbarx » 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.

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: 5435
Joined: Sat Feb 25, 2017 10:45 pm

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

Post by bogs » Mon Nov 05, 2018 8:12 pm

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 :oops:
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.
Image

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

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

Post by dunbarx » Tue Nov 06, 2018 2:55 am

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. :oops:

Keep the faith.

Craig

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

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

Post by bogs » Tue Nov 06, 2018 3:05 am

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 :wink:

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

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

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

Post by jmburnod » Tue Nov 06, 2018 10:10 am

keep the faith

and always doubt

jean-Marc (Old Baby)
https://alternatic.ch

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”