use strict; use Tk; use Data::Dumper; my $addy1 = '32'; my $addy2 = '42'; my $mw = MainWindow->new(-title => 'Mac Address Input Example'); my $fr = $mw->Frame()->pack(-expand => 1, -fill => 'both'); my $lb = $fr->Label(-text => 'MAC Address')->pack(-side => 'left'); my $e1 = $fr->Entry(); $e1->configure( -textvariable => \$addy1, -width => 2, -validate => 'focusout', -validatecommand => \&validate, -invalidcommand => [ \&show_invalid, $e1 ], ); $e1->pack(-side => 'left'); # Need a field to go to so 'focusout' can happen. my $e2 = $fr->Entry(); $e2->configure( -textvariable => \$addy2, -width => 2, -validate => 'focusout', -validatecommand => \&validate, -invalidcommand => [ \&show_invalid, $e2 ], ); $e2->pack(-side => 'left'); MainLoop; sub validate { my ($val) = @_; my $b_valid = ($val =~ /^[0-9a-f]{1,2}$/i)? 1: 0; # debugging, print the field. printf"<%s>: %s valid\n", $val, $b_valid? "IS": "is NOT"; return $b_valid; } sub show_invalid { my ($widget) = @_; $widget->focus(); my $bg = $widget->cget(-background); $widget->configure(-background => 'red'); $widget->update(); select(undef, undef, undef, 0.2); $widget->configure(-background => $bg); $widget->update(); }