in reply to Generic Entry widget validation
#!/usr/bin/perl use strict; use warnings; use Tk; use Regexp::Common qw/number/; my $top = MainWindow->new(); $top->geometry('200x200'); my %entries; for(1..4){ $entries{$_}{'value'} ||= 0.0; $entries{$_}{'entry'} = $top->Entry( -textvariable => \$entries{$_}{'value'}, -width => 15, -validate => 'key', -vcmd => \&validate, )->pack; } MainLoop; #have to make sure empty value has numeric context sub validate{ my $val = shift; $val ||= 0.0; #get real numbers only #this regex allows 2 decimal points....bad # if( $val !~ /^[\d+\.]+$/ ){ return 0 } if( $val !~ /^$RE{num}{real}$/ ) { return 0 } if (($val >= 0.0) and ($val <= 100.0)) {return 1} else{ return 0 } }
|
|---|