use CGI; use Data::Validate::OO; #EEK! We all know what that OO means! use strict; #of couse use warnings; #ditto #I'm making an instance of it... obviously... my $checker = new Data::Validate::OO( -failure => sub { my($field,$data) = @_; print "Data $data in $field illegal!"; } ); $checker->newrule( -name => 'homephone', -element => 'phone', #Not required, defaults to name -tests => { -custom =>[ qr/^\d{3}[\-\s]?\d{3}[\-\s]?\d{4}$/, #sorry if that's not quite right, writing it on the fly ], }, ); $checker->newrule( -name => 'name', -tests => { -custom => [ qr/\w*\s*\w\.?\s*\w*/, ], } ); $checker->newrule( -name=>'mail', -required => 0, #Defaults to 1, not required, still complains if data is there but fails tests -tests => { -def => 'email', #Use a built-in check for valid email -custom =>[ sub{ my $data = shift; if($data =~ m/\@hotmail\.com$/){ return; } return 1; } ], } ); #Ok, we have a simple check now, try using it. my $CGI = new CGI; my $status = $checker->test($CGI->param()); #Testing incoming form data! #or my $status = $checker->test(name=>'John R. Doe',phone=>'000-111-2345',mail=>'me@myhost.com'); #This should result in nothing being printed, and true being returned my $status = $checker->test(name=>'a',phone=>'3553451634',mail=>'me@myhost.com'); #Would complain about the name and return false my $status = $checker->test(name=>'My R. Name',phone=>'344-234-2525',mail=>'u@hotmail.com'); #Would fail because even though an email is no required, it was not one that could be accepted (not empty or valid) my $status = $checker->test(name=>'Your A. Name',phone=>'1323445432'); #This would pass because the email is not defined #Now that you know the data is or is not valid, do something!