Limit user input in a field...

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
tj333
Posts: 12
Joined: Sat Jun 25, 2016 11:46 am

Limit user input in a field...

Post by tj333 » Wed Jul 13, 2016 8:07 pm

I have an editable field that contains text like "1234-56". I want users to be able to click on the field and edit the last two digits after the dash, but not the first four digits. Is there a way to do this?

Thanks for any suggestions or advice,

TJ.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Limit user input in a field...

Post by dunbarx » Wed Jul 13, 2016 8:52 pm

Hi.

The best way to do something like this is not to allow the user to edit the field at all. But I do not know how text is entered into that field, so this might get dicey.

Anyway, just as a start, if we assume that the text is in the field by magic, and the field is locked, you can, in the field script

Code: Select all

on mouseUp
  set the itemDel to "-"
  ask "Modify?" with item 2 of me
  put it into item 2 of me
end mouseUp
Now if the field must open for text entry (fully editable), then let me know. We will have to explore other methods.

Craig Newman

Pistris
Posts: 148
Joined: Mon Jul 20, 2015 2:40 am

Re: Limit user input in a field...

Post by Pistris » Wed Jul 13, 2016 10:10 pm

you can use a label and a field and edit the visuals so they look like 1 field

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Limit user input in a field...

Post by dunbarx » Wed Jul 13, 2016 11:15 pm

How would you build that? Assuming the idea is to have a simple editable field, and now and again, the user is required to edit only the text after the dash.

There are ways, of course. Double clicking at the text after the dash will only select that portion. But this is fraught with peril, where the user could easily mess other text up.

How about this in an unlocked field:

Code: Select all

on mouseWithin
   if the mouse is down then
      set the itemDel to "-"
      ask "Modify?" with item 2 of the value of the mouseLine 
      put it into item 2 of me
   end if
end mouseWithin
Fun, eh? This is what I meant by "other methods"

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7215
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Limit user input in a field...

Post by jacque » Thu Jul 14, 2016 6:49 pm

You could try this handler in the field:

Code: Select all

on selectionChanged
  set the itemdel to "-"
  select item 2 of me
end selectionChanged
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Limit user input in a field...

Post by dunbarx » Thu Jul 14, 2016 7:46 pm

Cute, Jacque. But the text is selected, perhaps, just a bit TOO often?

TJ? What do you think? Just how interactive do you want this little gadget to be?

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7215
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Limit user input in a field...

Post by jacque » Thu Jul 14, 2016 8:35 pm

Maybe, I guess it depends on the usage as you say. Since there are only two numbers it didn't seem much of an inconvenience.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

paul_gr
Posts: 319
Joined: Fri Dec 08, 2006 7:38 pm
Location: Blenheim, New Zealand

Re: Limit user input in a field...

Post by paul_gr » Thu Jul 14, 2016 10:00 pm

Hi TJ,
Complexity depends on the amount of control you want the user to have.
See attached example.

it allows only two chars past the "-" and you can edit only after the "-".
backspace and delete work only past the "-"

Paul
Attachments
example.zip
(11.26 KiB) Downloaded 197 times

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Limit user input in a field...

Post by dunbarx » Fri Jul 15, 2016 5:48 am

Hi.

I did not see your stack, but the tone of the OP was that the text was "like" his example, so the limitations may not be pertinent. But all this is a hoot to play around with...

Craig

Lagi Pittas
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 365
Joined: Mon Jun 10, 2013 1:32 pm

Re: Limit user input in a field...

Post by Lagi Pittas » Fri Jul 15, 2016 12:28 pm

Hi

I've used paul's "framework" - but changed the basic way it works to use a mask field.
This allows the number of digits after the dash to be extended by adding 1 character to the mask also just for the hell of it I have allowed for different characters to be valid in the two places where they can change.

The code is pretty easy to follow IMNSHO :wink: and can be expanded greatly.

I do have a much more advanced version that works like the mask in Foxpro allowing for Dates and Currencies - I did this one on the fly as that code would be too big to show the logic and It would have probably taken longer to massage it. That code needs tidying up but it does everything I need for my specific project but there are things I want to clean up before I share it.

Anyway when I get a bit of time (HAH!!) I will tidy it up and put it on revonline (point taken Richard - although revonline is not really a good advert for LCs capabilities is it?).

Code: Select all

-- Allow all digits and A B and C in the second "digit" after the dash
constant kALLOWED = "0123456789ABC"
-- We allow only 6 valid characters in the first "digit" after the dash sign
constant kNUMBERS= "0123XYZ"
-- This is the Mask - Only the 9 and the A mean anything the rest are "fillers" but you can define your own
-- the 9 means only allow from kNUMBERS and the A means only allow from  kAllowed
constant kMask2Num = "____-9A"

function Allowed pKey
   local lnPos, lcMask, llAllow, lcMsg
   
   put the second word of the selectedchunk  into lnPos
   put character lnPos of kMask2Num into lcMask
   
   -- Don't allow anything longer than the mask string
   if lnPos  > Length(kMask2Num) then
      beep
      return false
   end if
   
   -- You can  do all sorts of logic here doesn't need a Mask so fill yer boots
   switch lcMask 
      case "9"
         put (pKey is in kNUMBERS) into llAllow
         put kNUMBERS into lcMsg
         break
      case "A"
         put (pKey is in kAllowed) into llAllow
         put kALLOWED into lcMsg
         break
      default
         -- They probably used the left arrow to move to the left of the dash sign 
         -- I haven't trapped that but all things are possible with LC
         put empty into lcMsg
         put false into llAllow
   end switch
   
   if llAllow is false then
      beep
      if lcMsg is NOT empty then
         Answer "Only the characters " && lcMsg && "Are allowed" with "Gotit"
      end if
   end if
   return llAllow
   
end Allowed

on keyDown pKey
   local lcTemp, lnPos
   
   -- You can do your own logic for where the cursor is etc
   if the length of field "fldDisplay" = length(kMask2Num) then
      delete the last character of field "fldDisplay"
      -- It now falls through and adds it to the end
   end if
   
   if Allowed(pKey) then
      -- Keep it clean by making letters uppercase
     put ToUpper(pKey) into the selectedchunk
   end if
   
end keyDown

on deleteKey
   local lnPos , lcTemp
   
   put field "fldDisplay" into lcTemp
   put the second word of the selectedchunk into lnPos
   if lnPos  > offset("-",lcTemp) then
      pass deletekey
   else
      select after character offset("-",kMask2Num) of field "fldDisplay"
      beep
   end if
end deleteKey

on backspaceKey
   local lnPos
   put the second word of the selectedchunk into lnPos
   if  lnPos  > offset("-",kMask2Num) + 1 then
      pass backspaceKey
   else
      select after character offset("-",kMask2Num) of field "fldDisplay"
      beep
   end if
end backspaceKey
Attachments
maskexample.zip
(10.41 KiB) Downloaded 178 times

tj333
Posts: 12
Joined: Sat Jun 25, 2016 11:46 am

Re: Limit user input in a field...

Post by tj333 » Fri Jul 15, 2016 9:56 pm

First off thanks so much to everyone who contributed here to help answer my question!

Laggi Pittas thanks for the code you posted. I have not had a chance to try it yet, but it looks cool and would seem to do exactly what I wanted and more with a bit of customization.

I have many new ideas for how to solve this issue. Thanks again to all who offered advice and assistance!

TJ.

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Limit user input in a field...

Post by [-hh] » Fri Jul 15, 2016 11:15 pm

Recently I had a similar "problem" with a currency field. I adjust my solution to your scenario.

The technique used is to delete the prefix part "1234-" from the text when the user opens the field, and to prepend this prefix part again to his/her edit when he/she exits or closes the field.

Code: Select all

local prefix, lprefix

on openfield
  put "1234-" into prefix
  put length(prefix) into lprefix
  put the text of me into t
  if char 1 to lprefix of t is prefix
  then delete char 1 to lprefix of t
  set text of me to t
end openfield

on closeField
  put char 1 to 2 of the text of me into t
  if t is not an integer or t<0 then put "00" into t
  if char 1 to lprefix of t is not prefix
  then set text of me to (prefix & t)
end closeField

on exitField
  closeField
end exitField
shiftLock happens

Pistris
Posts: 148
Joined: Mon Jul 20, 2015 2:40 am

Re: Limit user input in a field...

Post by Pistris » Sat Jul 16, 2016 4:48 am

Tj33 a simple hack is to draw the prefix numbers on top of the field where you want them with a simple label
You can set the label properties to any text you like and then restrict the field to 4 digits and align them to the right

It will look like all the digits are in the field

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”