Limit user input in a field...
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Limit user input in a field...
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.
Thanks for any suggestions or advice,
TJ.
-
- VIP Livecode Opensource Backer
- Posts: 9731
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Limit user input in a field...
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
Now if the field must open for text entry (fully editable), then let me know. We will have to explore other methods.
Craig Newman
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
Craig Newman
Re: Limit user input in a field...
you can use a label and a field and edit the visuals so they look like 1 field
-
- VIP Livecode Opensource Backer
- Posts: 9731
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Limit user input in a field...
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:
Fun, eh? This is what I meant by "other methods"
Craig
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
Craig
-
- VIP Livecode Opensource Backer
- Posts: 7257
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Limit user input in a field...
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
HyperActive Software | http://www.hyperactivesw.com
-
- VIP Livecode Opensource Backer
- Posts: 9731
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Limit user input in a field...
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
TJ? What do you think? Just how interactive do you want this little gadget to be?
Craig
-
- VIP Livecode Opensource Backer
- Posts: 7257
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Limit user input in a field...
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
HyperActive Software | http://www.hyperactivesw.com
Re: Limit user input in a field...
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
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 203 times
-
- VIP Livecode Opensource Backer
- Posts: 9731
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Limit user input in a field...
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
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
-
- VIP Livecode Opensource Backer
- Posts: 365
- Joined: Mon Jun 10, 2013 1:32 pm
Re: Limit user input in a field...
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 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?).
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 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 184 times
Re: Limit user input in a field...
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.
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.
-
- 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...
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.
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
Re: Limit user input in a field...
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
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