in reply to Re: how to use validatecommand for string checking
in thread how to use validatecommand for string checking

Thanks for your response Monks jdporter and zentara.
Your responses have confirmed my guess that the validatecommand doesn't check for string without us to do some fancy things. By the way, the reason I have to check for "go " in the beginning but avoid appendding "go " after reading the content of the Entry is because in fact I have to check for two keywords, not just one, "go " and "fly ". Depeding on which keyword the user enters, I will treat the data entered after these two keywords differently. I only mentioned "go " in the original post just for brevity.

So I based on your code and crafted one to hopefully do what I want. The code is written to the best of my knowledge below. However, it appears that the validatecommand only check ONCE in this case. For example, if I type "gh" it will flag error as expected. But after I delete "gh" and retype "gn" it won't flag error anymore! The print command inside the sub is even not invoked at the 2nd mistake I intended to make! Would you please help if you see if there's something wrong here?

#!/usr/bin/env perl use English; require Tk; use Tk; use Tk; my $mw = MainWindow->new(); my $entry = $mw->Entry()->pack(); $entry->configure( -textvariable => \$content , -validate => 'key' , -validatecommand => sub { my $newvalue = shift; my $changedchars = shift; my $currentvalue = shift; my $index = shift; my $type = shift; print "newval-> $newvalue\nchangedchars-> $changedchars\n". "curvalue-> $currentvalue\n index-> $index\n\n\n"; if( ($index == 0) and ($newvalue ne 'g')){return 0} if( ($index == 1) and ($newvalue ne 'go')){return 0} if( ($index == 2) and ($newvalue ne 'go ')){return 0} return 1; }, -invalidcommand => [ \&errorsub , \$entry ] ) ; MainLoop; sub errorsub { print "ERROR.\n"; $content = "" ; $entry->focus; $entry->icursor(0); }
Thanks,

Danny

Replies are listed 'Best First'.
Re^3: how to use validatecommand for string checking
by zentara (Cardinal) on Aug 27, 2007 at 18:39 UTC
    Another alternative which you may be overlooking, is to forget the -validate stuff, and just bind on the Return keypress, so you get the whole string at once. Then you can regex it.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new(); my $content = ''; my $entry = $mw->Entry(-textvariable => \$content)->pack(); $entry->bind('<Return>',sub{ my $val = $entry->get; print "check with regex here and do what is needed $val\n" }); $entry->focus; MainLoop;

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum