Filtering User input "key pressed" / Repeat for each ?

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
teriibi
Posts: 254
Joined: Mon Nov 13, 2017 3:49 pm
Location: Bolivia

Filtering User input "key pressed" / Repeat for each ?

Post by teriibi » Fri Feb 23, 2018 4:33 am

Hi,

I want to start establishing some kind of User inputs filtering control. :idea:
...so users can NOT type invalid Characters (or injecting code) within selected Text fields of my stack.

Code: Select all

replace "@" with empty in fld "Fname"
   replace "+" with empty in fld "Fname"
   replace "-" with empty in fld "Fname"
   replace "*" with empty in fld "Fname"
1) What would be the apropriate "action name" to "run" such a filter...?
(So far I have only been using the "on mouseup" action ! :P

2) Can the "replacement process" below be simplified using a "Repeat forEach" syntax, or there are better ways ?

if anyone´s got tracks to follow, I´m buying ! :mrgreen: :wink:

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

Re: Filtering User input "key pressed" / Repeat for each ?

Post by dunbarx » Fri Feb 23, 2018 5:06 am

Hi.

Why not go right to the source? In the field script:

Code: Select all

on keyDown tKey
   if tKey is not in "@+-*" then pass keyDown
end keyDown
Craig Newman

teriibi
Posts: 254
Joined: Mon Nov 13, 2017 3:49 pm
Location: Bolivia

Re: Filtering User input "key pressed" / Repeat for each ?

Post by teriibi » Fri Feb 23, 2018 5:41 am

Thanks, well, if I create a Form with several text field I thought it would be more easy to manage a single
rule, and apply it to all necessary fields to control.
I guess creating a function somewhere and specify it, then, to all ncessary fields to control.

Thanks for the syntax. :wink:

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

Re: Filtering User input "key pressed" / Repeat for each ?

Post by dunbarx » Fri Feb 23, 2018 7:28 am

Do you know that you can place that handler in the card or stack script, so that all fields will have the same functionality? In that way, you only need a single handler, and all fields will act similarly..

Try it. Make several fields, and move that handler to the card script. Make sure you clear the script of the original field. Now type here and there among the them all.

This may cause other issues. For example, perhaps you only want certain fields to exhibit this property. Do you see how to discriminate among them. so that only certain ones do? There are a handful of simple methods that will accomplish this. Jacque insists that you do this as a homework assignment.

Oh, and have you learned about behaviors?

Write back with all of your thoughts.

Craig

teriibi
Posts: 254
Joined: Mon Nov 13, 2017 3:49 pm
Location: Bolivia

Re: Filtering User input "key pressed" / Repeat for each ?

Post by teriibi » Fri Feb 23, 2018 2:41 pm

Yes, thats the idea, Applying :
- a Rule number 1 to field group N.1
- a Rule number 2 to field group N.2.
A simple reason (for using few instead of just one) being that 1 rule would process Input for numbers fld type... while a 2nd rule do the same but for text fld type. (possibly some other rules for special inputs...phone or email types...)

I m still lacking a lot about even "basic" capailities of LC, like Ive red about dealing with fld properties, which I have no idea (yet) how this works although I understand it might solve some needs too...
behaviors ? nop ! :roll:

PS: All my coding so far only involves "on mouse up/end" button use...nothing further yet. :oops:

...so, yeah, any guidelines to point me to the right Docs will be very welcome at this stage.
:x :arrow: :idea:

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

Re: Filtering User input "key pressed" / Repeat for each ?

Post by dunbarx » Fri Feb 23, 2018 4:35 pm

Did you try placing the handler in the card script and typing into a bunch of fields?

Do it.

Now then. The keyDown message is sent from the "target", which is the field currently being typed into. On a NEW card, make two new fields and a button. We are going to set a custom property for each field, called "prohibit".

In the button script::

Code: Select all

on mouseUp
set the prohibit of fld 1 to "AB"
set the prohibit of fld 2 to "CD"
end mouseUp
In the card script:

Code: Select all

on keyDown tKey
   if tKey is not in the prohibit of the target then pass keyDown
end keyDown
Click the button. Now type into each field, and see whether fld 1 will accept A or B, and whether fld 2 will accept C or D.

This is a method of separating fields into "classes", each class being able to filter only certain characters.The first class filters "AB", and the second class filters "CD". Any field can be added to any class by simply setting its prohibit property. The one line handler in the card script is all you need.

This is a compact handler that needs some thought. You may not be familiar with the parts of LC I am using, but you should be able to simply describe what is going on. So think about it, and write back with any theories you have. I want to show you the power of LiveCode, but do not want to go too fast for you. I need to know how well you are doing with this.

Craig

teriibi
Posts: 254
Joined: Mon Nov 13, 2017 3:49 pm
Location: Bolivia

Re: Filtering User input "key pressed" / Repeat for each ?

Post by teriibi » Fri Feb 23, 2018 10:25 pm

Cool, for sure I´ll test it and let you know next time...Tkx :wink:

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”