Page 1 of 1

Only number and one point after first number allowed?

Posted: Sat Nov 03, 2018 7:16 am
by AlessioForconi
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

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

Posted: Sat Nov 03, 2018 2:26 pm
by jmburnod
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

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

Posted: Sat Nov 03, 2018 4:56 pm
by AlessioForconi
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

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

Posted: Sat Nov 03, 2018 5:43 pm
by bogs
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

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

Posted: Mon Nov 05, 2018 5:32 pm
by dunbarx
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

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

Posted: Mon Nov 05, 2018 8:12 pm
by bogs
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.

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

Posted: Tue Nov 06, 2018 2:55 am
by dunbarx
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

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

Posted: Tue Nov 06, 2018 3:05 am
by bogs
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.

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

Posted: Tue Nov 06, 2018 10:10 am
by jmburnod
keep the faith

and always doubt

jean-Marc (Old Baby)