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


In reply to Re^2: how to use validatecommand for string checking by dannytrannox
in thread how to use validatecommand for string checking by dannytrannox

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.