Page 1 of 3

Limit character number in field

Posted: Sun Sep 20, 2009 7:16 am
by bidgeeman
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

Posted: Sun Sep 20, 2009 9:54 am
by malte
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

Posted: Sun Sep 20, 2009 11:58 pm
by bidgeeman
Hi malte.
Great! Thank you very much mate. I will check out the paste function too.

Cheers
Bidge

Re: Limit character number in field

Posted: Fri May 19, 2017 10:05 pm
by vascoribeiro
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

Re: Limit character number in field

Posted: Fri May 19, 2017 10:11 pm
by dunbarx
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

Re: Limit character number in field

Posted: Fri May 19, 2017 10:33 pm
by vascoribeiro
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!

Re: Limit character number in field

Posted: Sat May 20, 2017 12:37 am
by bidgeeman
For some reason I received an email that someone replied to one of my extremely old threads :)

Many thanks
Cheers
Bidge

Re: Limit character number in field

Posted: Sat May 20, 2017 2:04 am
by dunbarx
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

Re: Limit character number in field

Posted: Sat May 20, 2017 3:46 pm
by jacque
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

Re: Limit character number in field

Posted: Sat May 20, 2017 3:55 pm
by vascoribeiro
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!

Re: Limit character number in field

Posted: Sat May 20, 2017 4:05 pm
by vascoribeiro
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!

Re: Limit character number in field

Posted: Sat May 20, 2017 4:16 pm
by jacque
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.

Re: Limit character number in field

Posted: Sat May 20, 2017 4:31 pm
by vascoribeiro
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:

Re: Limit character number in field

Posted: Sat May 20, 2017 6:53 pm
by jacque
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

Re: Limit character number in field

Posted: Sat May 20, 2017 7:06 pm
by vascoribeiro
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!