Limit character number in field

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am
Location: Australia

Limit character number in field

Post by bidgeeman » Sun Sep 20, 2009 7:16 am

Hi.
I've looked through the dictionary and online but can't find anything that shows how to limit the number of characters in a text feld. Could someone possibly help?

Many thanks
Bidge

malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Location: Ostenfeld germany
Contact:

Post by malte » Sun Sep 20, 2009 9:54 am

Hey Bidge,

you can for sure script it.

on keyDown theKey
if the number of chars of me <10 then pass keyDown
end keyDown

You will want to make sure to trap pasting too. (look up pasteKey or of course feel free to ask here. :-) )

Hth,

Malte

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am
Location: Australia

Post by bidgeeman » Sun Sep 20, 2009 11:58 pm

Hi malte.
Great! Thank you very much mate. I will check out the paste function too.

Cheers
Bidge

vascoribeiro
Posts: 33
Joined: Tue Apr 25, 2017 8:13 pm

Re: Limit character number in field

Post by vascoribeiro » Fri May 19, 2017 10:05 pm

Hi there everyone!

Related to this topic: how do I limit the field to entries of real numbers from 0-100 (for example 25; 50; 74.5)?

Thank you for your help.
Vasco

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

Re: Limit character number in field

Post by dunbarx » Fri May 19, 2017 10:11 pm

Hi.

What you need to do is to draw back a bit from a simple keyboard entry, and validate the input as a whole. Might the "ask" command be useful here? Write back if you run into trouble.

Craig Newman

vascoribeiro
Posts: 33
Joined: Tue Apr 25, 2017 8:13 pm

Re: Limit character number in field

Post by vascoribeiro » Fri May 19, 2017 10:33 pm

dunbarx wrote:Hi.

What you need to do is to draw back a bit from a simple keyboard entry, and validate the input as a whole. Might the "ask" command be useful here? Write back if you run into trouble.

Craig Newman
Hi Craig,
The ask command opens a dialog right? In this case I think it would be better to set "if" conditions. I have a large number of fields that the user have to complete with percentages. I wanted to block the input...

I've something like this now:

on keyDown pKey

if pKey is a number or pKey is "." then
pass keyDown
else
beep
end if
end keyDown

I wanted the number to be less than 100, but cant figure out how to do it.

Thank you!

bidgeeman
Posts: 495
Joined: Wed Feb 21, 2007 7:58 am
Location: Australia

Re: Limit character number in field

Post by bidgeeman » Sat May 20, 2017 12:37 am

For some reason I received an email that someone replied to one of my extremely old threads :)

Many thanks
Cheers
Bidge

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

Re: Limit character number in field

Post by dunbarx » Sat May 20, 2017 2:04 am

Hmmm.

Not sure I understand your setup. But say you have a lot of fields. Are they locked? If so they will respond to "mouseUp" messages. If not, we will explore other ways. But if so, in the card script, you might have something like:

Code: Select all

on mouseUp
ask "Enter a number"
if it is a number and it >= 0 and it <= 100 then put it into the target
end mouseUp
This should be very readable. The "target", if you are not familiar, should be carefully studied and practiced. Now if the field is not locked, write back. Or rather lock them; you will likely want to.

Craig

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

Re: Limit character number in field

Post by jacque » Sat May 20, 2017 3:46 pm

if pKey is a number or pKey is "." then
Just add the third condition to the test:

Code: Select all

if (pKey is a number or pKey is ".") and pKey <= 100 then
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

vascoribeiro
Posts: 33
Joined: Tue Apr 25, 2017 8:13 pm

Re: Limit character number in field

Post by vascoribeiro » Sat May 20, 2017 3:55 pm

bidgeeman wrote:For some reason I received an email that someone replied to one of my extremely old threads :)

Many thanks
Cheers
Bidge
I am actually using it to make questions related to the topic! =D
I'm the one who should say thank you. ;)
dunbarx wrote:Hmmm.

Not sure I understand your setup. But say you have a lot of fields. Are they locked? If so they will respond to "mouseUp" messages. If not, we will explore other ways. But if so, in the card script, you might have something like:

Code: Select all

on mouseUp
ask "Enter a number"
if it is a number and it >= 0 and it <= 100 then put it into the target
end mouseUp
This should be very readable. The "target", if you are not familiar, should be carefully studied and practiced. Now if the field is not locked, write back. Or rather lock them; you will likely want to.

Craig
Craig,
Thank you so much for this, it's a great way around this problem, but I may have too many fields for this to be practical.

I have 36 fields, and not all of them have the same limits, so I may have to code them individually. All of them are unlocked and I wished that if the user didn't entered a number within the limits stablished in the code then the field remained empty. In so many fields I think it would be boring or annoying to the user answering to 36 dialogs (but I will use your idea to some of the fields - the dialog can add some relevant information for the user).

Thank you!

vascoribeiro
Posts: 33
Joined: Tue Apr 25, 2017 8:13 pm

Re: Limit character number in field

Post by vascoribeiro » Sat May 20, 2017 4:05 pm

jacque wrote:
if pKey is a number or pKey is "." then
Just add the third condition to the test:

Code: Select all

if (pKey is a number or pKey is ".") and pKey <= 100 then
Hi jacque!

I've tried that but no success... I can't get it to work. The behaviour is just as if I didn't add that to the code.

Thank you anyway!

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

Re: Limit character number in field

Post by jacque » Sat May 20, 2017 4:16 pm

Did you include the parentheses?

You can use a single handler in the card script if you store each field limit as a custom property in the field and have the "if" statement check the property instead of a hard coded number.

I agree that using a dialog in this situation isn't appropriate. All modern OSs discourage use of dialogs now anyway. You are on the right track.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

vascoribeiro
Posts: 33
Joined: Tue Apr 25, 2017 8:13 pm

Re: Limit character number in field

Post by vascoribeiro » Sat May 20, 2017 4:31 pm

jacque wrote:Did you include the parentheses?

You can use a single handler in the card script if you store each field limit as a custom property in the field and have the "if" statement check the property instead of a hard coded number.

I agree that using a dialog in this situation isn't appropriate. All modern OSs discourage use of dialogs now anyway. You are on the right track.
I did included the parentheses... And I've tried in different releases of LiveCode to test if it was some bug...
Thank you! :wink:

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

Re: Limit character number in field

Post by jacque » Sat May 20, 2017 6:53 pm

Now that I am at the computer and not answering off the top of my head, I see what is failing. Try this:

Code: Select all

on keydown pKey
  if (pKey is a number or pKey is ".") then
    if (me & pKey) <= 100 then
      pass keydown
    end if
  end if
end keydown
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

vascoribeiro
Posts: 33
Joined: Tue Apr 25, 2017 8:13 pm

Re: Limit character number in field

Post by vascoribeiro » Sat May 20, 2017 7:06 pm

jacque wrote:Now that I am at the computer and not answering off the top of my head, I see what is failing. Try this:

Code: Select all

on keydown pKey
  if (pKey is a number or pKey is ".") then
    if (me & pKey) <= 100 then
      pass keydown
    end if
  end if
end keydown
Thank you so so much, with this I can continue to improve my work!
I don't understand why it only worked with this method. Out of curiosity, why our last ideia:

Code: Select all

if (pKey is a number or pKey is ".") and pKey <= 100 then
;
didn't work?

Thanks to all!

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”