in reply to Re: Stopping bad input (harder than sanitizing)
in thread Stopping bad input (harder than sanitizing)

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.

Replies are listed 'Best First'.
Re^3: Stopping bad input (harder than sanitizing)
by swl (Prior) on Mar 09, 2021 at 06:44 UTC

    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.

        The hyphen in the middle is significant. Move it to the end if you want it to be a literal.


        🦛

        Please put perl code inside code tags, it's not possible to tell just what you entered.

      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