Page 1 of 1

Limit field to contain a max of 2 digits after decimal

Posted: Thu Mar 20, 2014 10:32 pm
by jalz
Hey all,

I'm trying to put some validation in data entry form. I've got the field accepting only numbers and 1 decimal point, however I also need to prevent the user typing in more than 2 digits once the decimal point is in place. Can someone help me with this calculation please?

Ultimately I'm trying to avoid anyone typing in 12345.678

Thanks
Jalz

Code: Select all

on keydown tKey
   if tKey is in "1234567890." then
      if (tKey is ".") and ("." is in me) then
         answer "You cannot have more than 1 decimal in this field"
      else
        -- if there is a "." and this digit is either 1 or 2 after the "." then
        pass keydown 
      end if
   end if
end keydown



Re: Limit field to contain a max of 2 digits after decimal

Posted: Thu Mar 20, 2014 11:42 pm
by dunbarx
Hi.
if there is a "." and this digit is either 1 or 2 after the "." then
Well, that is what is known as pseudocode, and very nice indeed.

Two possible methods. First, read in the dictionary about the "offset" function. Think about examining the current value of your field data with each keydown message. This function will allow you to further examine the string that is present after the decimal.

Another, similar way is to use the itemDelimiter, set to "." (period). The point of either of these attacks is to parse the current contents of the field to secure the decimal portion, if any. At that moment you can decide whether or not to allow the pending entry character.

It sounds like you should have no problem doing this. So do it both ways. Write back if you get stuck.

Craig Newman

EDIT. Your method of filtering out multiple decimal points might be made more elegant. Read also on the "is a" operator, in the context of (Pseudocode): "Is this a number". Strings such as "1.2.3" are not numbers to LiveCode. Get going....

Re: Limit field to contain a max of 2 digits after decimal

Posted: Thu Mar 20, 2014 11:43 pm
by bn
Hi Jalz,

try this:

Code: Select all

on keydown tKey
   if tKey is in "1234567890." then
      if "." is in me then
         if tKey is "." then
            answer "You cannot have more than 1 decimal in this field"
            exit keyDown
         else
            set the itemDelimiter to "."
            if length (item 2 of me) = 2 then exit keyDown
            pass keyDown
         end if
      end if
      pass keyDown
   end if
end keydown
Kind regards
Bernd

Re: Limit field to contain a max of 2 digits after decimal

Posted: Thu Mar 20, 2014 11:46 pm
by dunbarx
Aw, Bernd.

How is this guy going to learn? :D

Craig

Re: Limit field to contain a max of 2 digits after decimal

Posted: Thu Mar 20, 2014 11:56 pm
by bn
Hi Craig,

I did not see your post, posted 1 minute after you.

I am confident that Jalz will study the proposed solution and come up with a better one :)
You would do that, Jalz, wouldn't you?

Kind regards
Bernd

Re: Limit field to contain a max of 2 digits after decimal

Posted: Fri Mar 21, 2014 12:01 am
by dunbarx
Bernd.

Good idea.

Jalz.

The gauntlet has been thrown.

Craig

Re: Limit field to contain a max of 2 digits after decimal

Posted: Fri Mar 21, 2014 9:56 pm
by jalz
Hi Guys,

Thanks for the replies :D . I'll keep reading and trying to improve on the code I have - I always do

Jalz