Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Stopping bad input (harder than sanitizing)

by kcott (Archbishop)
on Mar 08, 2021 at 07:23 UTC ( [id://11129321]=note: print w/replies, xml ) Need Help??


in reply to Stopping bad input (harder than sanitizing)

See the Tk::Entry documentation. In particular, the -validate, -validatecommand and -invalidcommand options (in WIDGET-SPECIFIC OPTIONS) and the entire VALIDATION section.

Also see the "Validated entries and password fields." examples in the Widget Demo.

— Ken

Replies are listed 'Best First'.
Re^2: Stopping bad input (harder than sanitizing)
by Anonymous Monk on Mar 09, 2021 at 01:16 UTC

    I think I made some progress:

    $search = $top->Entry ('-width' => 20, '-validate' => 'key', '-validatecommand' => sub {$_[1] =~/\w+/ +;} , '-invalidcommand' => sub {$top -> bell() + ;}, ) ->pack('-side' => 'left');

    I found the code on 'Mastering Perl/Tk' -- I modified it to work with my program. But I still need it to accept ':' and '-'. As for invalid input it should do something like this:

    '-invalidcommand' => sub {$_[1] =~/^[[:cntrl:]] ; $top -> bell; }
    But it sill locks up...

    2021-03-09 Athanasius added code tags around final snippet.

      You could use a character class to accept : and - in the validatecommand sub.

      '-validatecommand' => sub {$_[1] =~/[\w:-]+/ ;} ,

      WRT your invalidcommand sub, the regexp does nothing at the moment. Should the first line be something like this if you want to accept control characters?

      '-invalidcommand' => sub { return if $_[1] =~/^[[:cntrl:]] ; $top -> bell; }

      Or do you want the bell if it starts with a control character?

      '-invalidcommand' => sub { $top -> bell if $_[1] =~/^[[:cntrl:]] ; }

      I don't code with Tk, but hopefully this is useful.

        Here is something new: I also wanna catch '[' (for [(1)). But when my expression is $_1 =~ /^[\w\s:-\[]+/ it spits out an error and says "stack moved" (is that PERL humor?). How do I add '[' to the character class? When I press <RET> with no data, the program still locks up.

        Gonna try those two ideas right now. My goal is two fold: 1) if this character-set, then its valid 2) if it begins with a control char (in particular <RET> ) then complain

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11129321]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-20 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found