Page 1 of 1

percentage validation

Posted: Sun Jan 25, 2015 12:29 am
by jalz
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

Re: percentage validation

Posted: Sun Jan 25, 2015 12:34 am
by Simon
if (me > 0) and (me < 100) then

Code: Select all

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

Simon

Re: percentage validation

Posted: Sun Jan 25, 2015 12:43 am
by magice
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

Re: percentage validation

Posted: Sun Jan 25, 2015 12:46 am
by Simon
Oh I get it now.

Simon

Re: percentage validation

Posted: Sun Jan 25, 2015 12:47 am
by jalz
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

Re: percentage validation

Posted: Sun Jan 25, 2015 12:58 am
by magice
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.

Re: percentage validation

Posted: Sun Jan 25, 2015 1:04 am
by jalz
Thanks Magice,
Missed the post with code, thanks it works, I'll start adding some more rules to it.
Jalz

Re: percentage validation

Posted: Sun Jan 25, 2015 1:08 am
by magice
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.

Re: percentage validation

Posted: Sun Jan 25, 2015 1:12 am
by Simon
OK there was some weirdness but now another
How come nothing shows in my field?

Simon

Re: percentage validation

Posted: Sun Jan 25, 2015 8:38 pm
by jacque
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

Re: percentage validation

Posted: Sun Jan 25, 2015 11:19 pm
by jalz
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

Re: percentage validation

Posted: Mon Jan 26, 2015 9:49 pm
by jacque
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.