Limit field to contain a max of 2 digits after decimal

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
jalz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 340
Joined: Fri Sep 12, 2008 11:04 pm

Limit field to contain a max of 2 digits after decimal

Post by jalz » Thu Mar 20, 2014 10:32 pm

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



dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

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

Post by dunbarx » Thu Mar 20, 2014 11:42 pm

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....
Last edited by dunbarx on Thu Mar 20, 2014 11:48 pm, edited 2 times in total.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

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

Post by bn » Thu Mar 20, 2014 11:43 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

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

Post by dunbarx » Thu Mar 20, 2014 11:46 pm

Aw, Bernd.

How is this guy going to learn? :D

Craig

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

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

Post by bn » Thu Mar 20, 2014 11:56 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

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

Post by dunbarx » Fri Mar 21, 2014 12:01 am

Bernd.

Good idea.

Jalz.

The gauntlet has been thrown.

Craig

jalz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 340
Joined: Fri Sep 12, 2008 11:04 pm

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

Post by jalz » Fri Mar 21, 2014 9:56 pm

Hi Guys,

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

Jalz

Post Reply