percentage validation

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

percentage validation

Post by jalz » Sun Jan 25, 2015 12:29 am

Hi Guys,

I've got the following code below, but its not quite working how it should as it triggers immediately once I have types in a number. I'm trying to to put some validation in one of my fields where the minimum value can be 1 and the maximum value can be 100, ideally percentage values with 1 decimal place but I'll add this later once I can get this range working.

Thanks
Jalz

Code: Select all

on keydown tKey
   if tKey is in "1234567890." then
      if (me > 0) and (me < 100) then
         pass keyDown
      else
         answer "100 is the maximum limit"
         delete the last character of me
         exit keyDown
      end if
   end if
end keydown

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: percentage validation

Post by Simon » Sun Jan 25, 2015 12:34 am

if (me > 0) and (me < 100) then

Code: Select all

if (tKey> 0) and (tKey< 100) then
:)

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: percentage validation

Post by magice » Sun Jan 25, 2015 12:43 am

try it with keyUp

Code: Select all

on keyUp tKey
   if tKey is in "1234567890." then
      if (me > 0) and (me < 100) then
         pass keyUp
      else
         answer "100 is the maximum limit"
         delete the last character of me
         exit keyUp
      end if
   end if
end keyUp
Last edited by magice on Sun Jan 25, 2015 12:56 am, edited 2 times in total.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: percentage validation

Post by Simon » Sun Jan 25, 2015 12:46 am

Oh I get it now.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

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

Re: percentage validation

Post by jalz » Sun Jan 25, 2015 12:47 am

Hi Simon & Magice,

I've tried that and indeed it passes through the number into the field. The thing is it bypasses the 100 limit after the first time it fails the validation limit, I'm assuming tKey is just validating against each number I type in rather than the entire contents of the field.
Magice, tried it with on KeyUp an i'm encountering the same issue.

Thanks
Jalz

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: percentage validation

Post by magice » Sun Jan 25, 2015 12:58 am

jalz wrote:Hi Simon & Magice,

I've tried that and indeed it passes through the number into the field. The thing is it bypasses the 100 limit after the first time it fails the validation limit, I'm assuming tKey is just validating against each number I type in rather than the entire contents of the field.
Magice, tried it with on KeyUp an i'm encountering the same issue.

Thanks
Jalz
The code I posted above works for me, the only problem is if someone tries to enter a decimal first.

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

Re: percentage validation

Post by jalz » Sun Jan 25, 2015 1:04 am

Thanks Magice,
Missed the post with code, thanks it works, I'll start adding some more rules to it.
Jalz

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: percentage validation

Post by magice » Sun Jan 25, 2015 1:08 am

jalz wrote:Thanks Magice,
Missed the post with code, thanks it works, I'll start adding some more rules to it.
Jalz
If I'm not mistaken, the reason it works on keyUp and not on keyDown, is that on keyDown the key value has not yet registered in the field, so "me" evaluates as empty.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: percentage validation

Post by Simon » Sun Jan 25, 2015 1:12 am

OK there was some weirdness but now another
How come nothing shows in my field?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: percentage validation

Post by jacque » Sun Jan 25, 2015 8:38 pm

You were on the right track using keyDown. KeyDown triggers before the character gets into the field, so you can trap those you don't want. There's no need to delete anything from the field that way.

Your original script will work if you concatenate the proposed character to the existing content before evaluating what the final content would be:

Code: Select all

on keydown tKey
  if tKey is in "1234567890." then
    put me & tKey into tVal -- see what would happen if this char is added
    if (tVal > 0) and (tVal < 100) then
      pass keyDown
    else
      answer "100 is the maximum limit"
    end if
  end if
end keydown
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: percentage validation

Post by jalz » Sun Jan 25, 2015 11:19 pm

Hi Guys,

Thanks all for chiming in with responses.

Here is my final code which seems to cover all the rules I want, do you guys think I'm missing anything? It should accept input such as 17.5, 100, 20 and shouldn't accept the enter key.

Thanks
Jalz

Code: Select all

on keydown tKey
   if tKey is in "1234567890." then
      -- dont accept any more characters if the field already contains 100
      if (me = 100) then
         exit keyDown
      end if
      
      put me & tKey into tVal
      
      -- dont accept more than 1 decimal
      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) = 1 then exit keyDown
            pass keyDown
         end if
      end if
      
      -- make sure the value in the field is between 1 and 100
      if (tVal > 0) and (tVal <= 100) then
         pass keyDown
      else
         answer "100 is the maximum limit"
      end if
   end if
end keydown

on returnInField
  -- do not pass
end returnInField

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: percentage validation

Post by jacque » Mon Jan 26, 2015 9:49 pm

Looks good to me. The only thing you might want to add is an enterInField handler like the one you already have for returnInField. LC distinguishes between the return key and the enter key, so if you want to trap both you need to do it with both handlers.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply