| 1 | # | ||
| 2 | # FormValidator.pm - Object that validates form input data. | ||
| 3 | # | ||
| 4 | # This file is part of FormValidator. | ||
| 5 | # | ||
| 6 | # Author: Francis J. Lacoste <francis.lacoste@iNsu.COM> | ||
| 7 | # | ||
| 8 | # Copyright (C) 1999 Francis J. Lacoste, iNsu Innovations | ||
| 9 | # Parts Copyright 1996-1999 by Michael J. Heins <mike@heins.net> | ||
| 10 | # Parts Copyright 1996-1999 by Bruce Albrecht <bruce.albrecht@seag.fingerhut.com> | ||
| 11 | # | ||
| 12 | # Parts of this module are based on work by | ||
| 13 | # Bruce Albrecht, <bruce.albrecht@seag.fingerhut.com> contributed to | ||
| 14 | # MiniVend. | ||
| 15 | # | ||
| 16 | # Parts also based on work by Michael J. Heins <mikeh@minivend.com> | ||
| 17 | # | ||
| 18 | # This program is free software; you can redistribute it and/or modify | ||
| 19 | # it under the terms same terms as perl itself. | ||
| 20 | # | ||
| 21 | package HTML::FormValidator; | ||
| 22 | |||
| 23 | use strict; | ||
| 24 | |||
| 25 | use vars qw( $VERSION ); | ||
| 26 | |||
| 27 | BEGIN { | ||
| 28 | ($VERSION) = '$Revision: 1.7 $' =~ /Revision: ([\d.]+)/; | ||
| 29 | } | ||
| 30 | |||
| 31 | =pod | ||
| 32 | |||
| 33 | =head1 NAME | ||
| 34 | |||
| 35 | HTML::FormValidator - Validates user input (usually from an HTML form) based | ||
| 36 | on input profile. | ||
| 37 | |||
| 38 | =head1 SYNOPSIS | ||
| 39 | |||
| 40 | In an HTML::Empberl page: | ||
| 41 | |||
| 42 | use HTML::FormValidator; | ||
| 43 | |||
| 44 | my $validator = new HTML::FormValidator( "/home/user/input_profiles.pl" ); | ||
| 45 | my ( $valid, $missing, $invalid, $unknown ) = $validator->validate( \%fdat, "customer_infos" ); | ||
| 46 | |||
| 47 | =head1 DESCRIPTION | ||
| 48 | |||
| 49 | HTML::FormValidator's main aim is to make the tedious coding of input | ||
| 50 | validation expressible in a simple format and to let the programmer focus | ||
| 51 | on more interesting task. | ||
| 52 | |||
| 53 | When you are coding web application one of the most tedious though | ||
| 54 | crucial task is to validate user's input (usually submitted by way of | ||
| 55 | an HTML form). You have to check that each required fields is present | ||
| 56 | and that some feed have valid data. (Does the phone input looks like a | ||
| 57 | phone number ? Is that a plausible email address ? Is the YY state | ||
| 58 | valid ? etc.) For simple form, this is not really a problem but as | ||
| 59 | forms get more complex and you code more of them this task became | ||
| 60 | really boring and tedious. | ||
| 61 | |||
| 62 | HTML::FormValidator lets you defines profiles which defines the | ||
| 63 | required fields and their format. When you are ready to validate the | ||
| 64 | user's input, you tell HTML::FormValidator the profile to apply to the | ||
| 65 | user data and you get the valid fields, the name of the fields which | ||
| 66 | are missing, the name of the fields that contains invalid input and | ||
| 67 | the name of the fields that are unknown to this profile. | ||
| 68 | |||
| 69 | You are then free to use this information to build a nice display to | ||
| 70 | the user telling which fields that he forgot to fill. | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | sub new { | ||
| 75 | my $proto = shift; | ||
| 76 | my $class = ref $proto || $proto; | ||
| 77 | |||
| 78 | my $profile_file = shift; | ||
| 79 | my $profiles = undef; | ||
| 80 | |||
| 81 | if ( ref $profile_file ) { | ||
| 82 | # Profile already passed as an hash reference. | ||
| 83 | $profiles = $profile_file; | ||
| 84 | $profile_file = undef; | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | bless { profile_file => $profile_file, | ||
| 89 | profiles => $profiles, | ||
| 90 | }, $class; | ||
| 91 | } | ||
| 92 | |||
| 93 | =pod | ||
| 94 | |||
| 95 | =head1 INPUT PROFILE SPECIFICATION | ||
| 96 | |||
| 97 | To create a HTML::FormValidator, use the following : | ||
| 98 | |||
| 99 | my $validator = new HTML::FormValidator( $input_profile ); | ||
| 100 | |||
| 101 | Where $input_profile may either be an hash reference to an input | ||
| 102 | profiles specification or a file that will be evaluated at runtime to | ||
| 103 | get a hash reference to an input profiles specification. | ||
| 104 | |||
| 105 | The input profiles specification is an hash reference where each key | ||
| 106 | is the name of the input profile and each value is another hash | ||
| 107 | reference which contains the actual profile elements. If the input | ||
| 108 | profile is specified as a file name, the profiles will be reread each | ||
| 109 | time that the disk copy is modified. | ||
| 110 | |||
| 111 | Here is an example of a valid input profiles specification : | ||
| 112 | |||
| 113 | { | ||
| 114 | customer_infos => { | ||
| 115 | optional => | ||
| 116 | [ qw( company fax country ) ], | ||
| 117 | required => | ||
| 118 | [ qw( fullname phone email address city state zipcode ) ], | ||
| 119 | constraints => | ||
| 120 | { | ||
| 121 | email => "email", | ||
| 122 | fax => "american_phone", | ||
| 123 | phone => "american_phone", | ||
| 124 | zipcode => '/^\s*\d{5}(?:[-]\d{4})?\s*$/', | ||
| 125 | state => "state", | ||
| 126 | }, | ||
| 127 | defaults => { | ||
| 128 | country => "USA", | ||
| 129 | }, | ||
| 130 | }, | ||
| 131 | customer_billing_infos => { | ||
| 132 | optional => [ "cc_no" ], | ||
| 133 | dependencies => { | ||
| 134 | "cc_no" => [ qw( cc_type cc_exp ) ], | ||
| 135 | }, | ||
| 136 | constraints => { | ||
| 137 | cc_no => { constraint => "cc_number", | ||
| 138 | params => [ qw( cc_no cc_type ) ], | ||
| 139 | }, | ||
| 140 | cc_type => "cc_type", | ||
| 141 | cc_exp => "cc_exp", | ||
| 142 | } | ||
| 143 | filters => [ "trim" ], | ||
| 144 | field_filters => { cc_no => "digit" }, | ||
| 145 | }, | ||
| 146 | } | ||
| 147 | |||
| 148 | The following are the valid fields for an input specification : | ||
| 149 | |||
| 150 | =over | ||
| 151 | |||
| 152 | =item required | ||
| 153 | |||
| 154 | This is an array reference which contains the name of the fields which | ||
| 155 | are required. Any fields in this list which are not present in the | ||
| 156 | user input will be reported as missing. | ||
| 157 | |||
| 158 | =item optional | ||
| 159 | |||
| 160 | This is an array reference which contains the name of optional fields. | ||
| 161 | These are fields which MAY be present and if they are, they will be | ||
| 162 | check for valid input. Any fields not in optional or required list | ||
| 163 | will be reported as unknown. | ||
| 164 | |||
| 165 | =item dependencies | ||
| 166 | |||
| 167 | This is an hash reference which contains dependencies information. | ||
| 168 | This is for the case where one optional fields has other requirements. | ||
| 169 | For example, if you enter your credit card number, the field cc_exp | ||
| 170 | and cc_type should also be present. Any fields in the dependencies | ||
| 171 | list that is missing when the target is present will be reported as | ||
| 172 | missing. | ||
| 173 | |||
| 174 | =item defaults | ||
| 175 | |||
| 176 | This is an hash reference which contains defaults which should be | ||
| 177 | substituted if the user hasn't filled the fields. Key is field name | ||
| 178 | and value is default value which will be returned in the list of valid | ||
| 179 | fields. | ||
| 180 | |||
| 181 | =item filters | ||
| 182 | |||
| 183 | This is a reference to an array of filters that will be applied to ALL | ||
| 184 | optional or required fields. This can be the name of a builting filter | ||
| 185 | (trim,digit,etc) or an anonymous subroutine which should take one parameter, the field value and return the (possibly) modified value. | ||
| 186 | |||
| 187 | =item field_filters | ||
| 188 | |||
| 189 | This is a reference to an hash which contains reference to array of | ||
| 190 | filters which will be apply to specific input fields. The key of the | ||
| 191 | hash is the name of the input field and the valud is a reference to an | ||
| 192 | array of filters like for the filters parameter. | ||
| 193 | |||
| 194 | =item constraints | ||
| 195 | |||
| 196 | This is a reference to an hash which contains the constraints that | ||
| 197 | will be used to check wheter or not the field contains valid data. | ||
| 198 | Constraint can be either the name of a builtin constraint function | ||
| 199 | (see below), a perl regexp or an anonymous subroutine which will check | ||
| 200 | the input and return true or false depending on the input's validity. | ||
| 201 | |||
| 202 | The constraint function takes one parameter, the input to be validated | ||
| 203 | and returns true or false. It is possible to specify the parameters | ||
| 204 | that will be passed to the subroutine. For that use an hash reference | ||
| 205 | which contains in the I<constraint> element, the anonymous subroutine | ||
| 206 | or the name of the builtin and in the I<params> element the name of | ||
| 207 | the fields to pass a parameter to the function. (Don't forget to | ||
| 208 | include the name of the field to check in that list!) For an example, | ||
| 209 | look at the I<cc_no> constraint example. | ||
| 210 | |||
| 211 | =back | ||
| 212 | |||
| 213 | =cut | ||
| 214 | |||
| 215 | sub load_profiles { | ||
| 216 | my $self = shift; | ||
| 217 | |||
| 218 | my $file = $self->{profile_file}; | ||
| 219 | return unless $file; | ||
| 220 | |||
| 221 | die "No such file: $file\n" unless -f $file; | ||
| 222 | die "Can't read $file\n" unless -r _; | ||
| 223 | |||
| 224 | my $mtime = (stat _)[9]; | ||
| 225 | return if $self->{profiles} and $self->{profiles_mtime} <= $mtime; | ||
| 226 | |||
| 227 | $self->{profiles} = do $file; | ||
| 228 | die "Error in input profiles: $@\n" if $@; | ||
| 229 | die "Input profiles didn't return an hash ref\n" | ||
| 230 | unless ref $self->{profiles} eq "HASH"; | ||
| 231 | |||
| 232 | $self->{profiles_mtime} = $mtime; | ||
| 233 | } | ||
| 234 | |||
| 235 | =pod | ||
| 236 | |||
| 237 | =head1 VALIDATING INPUT | ||
| 238 | |||
| 239 | my( $valids, $missings, $invalids, $unknowns ) = | ||
| 240 | $validator->validate( \%fdat, "customer_infos" ); | ||
| 241 | |||
| 242 | To validate input you use the validate() method. This method takes two | ||
| 243 | parameters : | ||
| 244 | |||
| 245 | =over | ||
| 246 | |||
| 247 | =item data | ||
| 248 | |||
| 249 | Contains an hash which should correspond to the form input as | ||
| 250 | submitted by the user. This hash is not modified by the call to validate. | ||
| 251 | |||
| 252 | =item profile | ||
| 253 | |||
| 254 | Can be either a name which will be used to lookup the corresponding profile | ||
| 255 | in the input profiles specification, or it can be an hash reference to the | ||
| 256 | input profile which should be used. | ||
| 257 | |||
| 258 | =back | ||
| 259 | |||
| 260 | This method returns a 4 elements array. | ||
| 261 | |||
| 262 | =over | ||
| 263 | |||
| 264 | =item valids | ||
| 265 | |||
| 266 | This is an hash reference to the valid fields which were submitted in | ||
| 267 | the data. The data may have been modified by the various filters specified. | ||
| 268 | |||
| 269 | =item missings | ||
| 270 | |||
| 271 | This is a reference to an array which contains the name of the missing | ||
| 272 | fields. Those are the fields that the user forget to fill or filled | ||
| 273 | with space. These fields may comes from the I<required> list or the | ||
| 274 | I<dependencies> list. | ||
| 275 | |||
| 276 | =item invalids | ||
| 277 | |||
| 278 | This is a reference to an array which contains the name of the fields | ||
| 279 | which failed their constraint check. | ||
| 280 | |||
| 281 | =item unknowns | ||
| 282 | |||
| 283 | This is a list of fields which are unknown to the profile. Whether or | ||
| 284 | not this indicates an error in the user input is application | ||
| 285 | dependant. | ||
| 286 | |||
| 287 | =back | ||
| 288 | |||
| 289 | =cut | ||
| 290 | |||
| 291 | sub validate { | ||
| 292 | my ( $self, $data, $name ) = @_; | ||
| 293 | |||
| 294 | my $profile; | ||
| 295 | if ( ref $name ) { | ||
| 296 | $profile = $name; | ||
| 297 | } else { | ||
| 298 | $self->load_profiles; | ||
| 299 | $profile = $self->{profiles}{$name}; | ||
| 300 | die "No such profile $name\n" unless $profile; | ||
| 301 | } | ||
| 302 | die "Invalid input profile\n" unless ref $profile eq "HASH"; | ||
| 303 | |||
| 304 | # Copy data and assumes that all is valid | ||
| 305 | my %valid = %$data; | ||
| 306 | my @missings = (); | ||
| 307 | my @invalid = (); | ||
| 308 | my @unknown = (); | ||
| 309 | |||
| 310 | # Apply inconditional filters | ||
| 311 | foreach my $filter ( @{$profile->{filters}} ) { | ||
| 312 | # Qualify symbolic references | ||
| 313 | $filter = ref $filter ? $filter : "filter_" . $filter; | ||
| 314 | foreach my $field ( keys %valid ) { | ||
| 315 | no strict 'refs'; | ||
| 316 | $valid{$field} = $filter->( $valid{$field} ); | ||
| 317 | } | ||
| 318 | } | ||
| 319 | |||
| 320 | # Apply specific filters | ||
| 321 | while ( my ($field,$filters) = each %{$profile->{field_filters} }) { | ||
| 322 | my @f = ref $filters eq "ARRAY" ? @$filters : ( $filters ); | ||
| 323 | foreach my $filter ( @f ) { | ||
| 324 | # Qualify symbolic references | ||
| 325 | $filter = ref $filter ? $filter : "filter_" . $filter; | ||
| 326 | no strict 'refs'; | ||
| 327 | |||
| 328 | $valid{$field} = $filter->( $valid{$field} ); | ||
| 329 | } | ||
| 330 | } | ||
| 331 | |||
| 332 | # Remove all empty fields | ||
| 333 | foreach my $field ( keys %valid ) { | ||
| 334 | delete $valid{$field} unless length $valid{$field}; | ||
| 335 | } | ||
| 336 | |||
| 337 | my %required = map { $_ => 1 } @{$profile->{required}}; | ||
| 338 | my %optional = map { $_ => 1 } @{$profile->{optional}}; | ||
| 339 | |||
| 340 | # Check if the presence of some fields makes other optional | ||
| 341 | # fields required. | ||
| 342 | while ( my ( $field, $deps) = each %{$profile->{dependencies}} ) { | ||
| 343 | if ( $valid{$field} ) { | ||
| 344 | foreach my $dep ( @$deps ) { | ||
| 345 | $required{$dep} = 1; | ||
| 346 | } | ||
| 347 | } | ||
| 348 | } | ||
| 349 | |||
| 350 | # Find unknown | ||
| 351 | @unknown = | ||
| 352 | grep { not (exists $optional{$_} or exists $required{$_} ) } keys %valid; | ||
| 353 | # and remove them from the list | ||
| 354 | foreach my $field ( @unknown ) { | ||
| 355 | delete $valid{$field}; | ||
| 356 | } | ||
| 357 | |||
| 358 | # Fill defaults | ||
| 359 | while ( my ($field,$value) = each %{$profile->{defaults}} ) { | ||
| 360 | $valid{$field} = $value unless exists $valid{$field}; | ||
| 361 | } | ||
| 362 | |||
| 363 | # Check for required fields | ||
| 364 | foreach my $field ( keys %required ) { | ||
| 365 | push @missings, $field unless exists $valid{$field}; | ||
| 366 | } | ||
| 367 | |||
| 368 | # Check constraints | ||
| 369 | while ( my ($field,$constraint_spec) = each %{$profile->{constraints}} ) { | ||
| 370 | my ($constraint,@params); | ||
| 371 | if ( ref $constraint_spec eq "HASH" ) { | ||
| 372 | $constraint = $constraint_spec->{constraint}; | ||
| 373 | foreach my $fname ( @{$constraint_spec->{params} } ) { | ||
| 374 | push @params, $valid{$fname}; | ||
| 375 | } | ||
| 376 | } else { | ||
| 377 | $constraint = $constraint_spec; | ||
| 378 | @params = ( $valid{$field} ); | ||
| 379 | } | ||
| 380 | next unless exists $valid{$field}; | ||
| 381 | |||
| 382 | unless ( ref $constraint ) { | ||
| 383 | # Check for regexp constraint | ||
| 384 | if ( $constraint =~ m@^\s*(/.+/|m(.).+\2)[cgimosx]*\s*$@ ) { | ||
| 385 | my $sub = eval 'sub { $_[0] =~ '. $constraint . '}'; | ||
| 386 | die "Error compiling regular expression $constraint: $@" if $@; | ||
| 387 | $constraint = $sub; | ||
| 388 | # Cache for next use | ||
| 389 | if ( ref $constraint_spec eq "HASH" ) { | ||
| 390 | $constraint_spec->{constraint} = $sub; | ||
| 391 | } else { | ||
| 392 | $profile->{constraints}{$field} = $sub; | ||
| 393 | } | ||
| 394 | } else { | ||
| 395 | # Qualify symbolic reference | ||
| 396 | $constraint = "valid_" . $constraint; | ||
| 397 | } | ||
| 398 | } | ||
| 399 | no strict 'refs'; | ||
| 400 | |||
| 401 | unless ( $constraint->( @params ) ) { | ||
| 402 | delete $valid{$field}; | ||
| 403 | push @invalid, $field; | ||
| 404 | } | ||
| 405 | } | ||
| 406 | |||
| 407 | return ( \%valid, \@missings, \@invalid, \@unknown ); | ||
| 408 | } | ||
| 409 | |||
| 410 | =pod | ||
| 411 | |||
| 412 | =head1 INPUT FILTERS | ||
| 413 | |||
| 414 | These are the builtin filters which may be specified as name in the | ||
| 415 | I<filters> and I<field_filters> parameters of the input profile. | ||
| 416 | |||
| 417 | =over | ||
| 418 | |||
| 419 | =item trim | ||
| 420 | |||
| 421 | Remove white space at the front and end of the fields. | ||
| 422 | |||
| 423 | =cut | ||
| 424 | |||
| 425 | sub filter_trim { | ||
| 426 | my $value = shift; | ||
| 427 | |||
| 428 | # Remove whitespace at the front | ||
| 429 | $value =~ s/^\s+//g; | ||
| 430 | |||
| 431 | # Remove whitespace at the end | ||
| 432 | $value =~ s/\s+$//g; | ||
| 433 | |||
| 434 | return $value; | ||
| 435 | } | ||
| 436 | |||
| 437 | =pod | ||
| 438 | |||
| 439 | =item strip | ||
| 440 | |||
| 441 | Runs of white space are replaced by a single space. | ||
| 442 | |||
| 443 | =cut | ||
| 444 | |||
| 445 | sub filter_strip { | ||
| 446 | my $value = shift; | ||
| 447 | |||
| 448 | # Strip whitespace | ||
| 449 | $value =~ s/\s+/ /g; | ||
| 450 | |||
| 451 | return $value; | ||
| 452 | } | ||
| 453 | |||
| 454 | =pod | ||
| 455 | |||
| 456 | =item digit | ||
| 457 | |||
| 458 | Remove non digits characters from the input. | ||
| 459 | |||
| 460 | =cut | ||
| 461 | |||
| 462 | sub filter_digit { | ||
| 463 | my $value = shift; | ||
| 464 | $value =~ s/\D//g; | ||
| 465 | |||
| 466 | return $value; | ||
| 467 | } | ||
| 468 | |||
| 469 | =pod | ||
| 470 | |||
| 471 | =item alphanum | ||
| 472 | |||
| 473 | Remove non alphanumerical characters from the input. | ||
| 474 | |||
| 475 | =cut | ||
| 476 | |||
| 477 | sub filter_alphanum { | ||
| 478 | my $value = shift; | ||
| 479 | $value =~ s/\W//g; | ||
| 480 | return $value; | ||
| 481 | } | ||
| 482 | |||
| 483 | =pod | ||
| 484 | |||
| 485 | =item integer | ||
| 486 | |||
| 487 | Extract from its input a valid integer number. | ||
| 488 | |||
| 489 | =cut | ||
| 490 | |||
| 491 | sub filter_integer { | ||
| 492 | my $value = shift; | ||
| 493 | $value =~ tr/0-9+-//dc; | ||
| 494 | ($value) =~ m/([-+]?\d+)/; | ||
| 495 | return $value; | ||
| 496 | } | ||
| 497 | |||
| 498 | =pod | ||
| 499 | |||
| 500 | =item pos_integer | ||
| 501 | |||
| 502 | Extract from its input a valid positive integer number. | ||
| 503 | |||
| 504 | =cut | ||
| 505 | |||
| 506 | sub filter_pos_integer { | ||
| 507 | my $value = shift; | ||
| 508 | $value =~ tr/0-9+//dc; | ||
| 509 | ($value) =~ m/(\+?\d+)/; | ||
| 510 | return $value; | ||
| 511 | } | ||
| 512 | |||
| 513 | =pod | ||
| 514 | |||
| 515 | =item pos_integer | ||
| 516 | |||
| 517 | Extract from its input a valid negative integer number. | ||
| 518 | |||
| 519 | =cut | ||
| 520 | |||
| 521 | sub filter_neg_integer { | ||
| 522 | my $value = shift; | ||
| 523 | $value =~ tr/0-9-//dc; | ||
| 524 | ($value) =~ m/(-\d+)/; | ||
| 525 | return $value; | ||
| 526 | } | ||
| 527 | |||
| 528 | =pod | ||
| 529 | |||
| 530 | =item decimal | ||
| 531 | |||
| 532 | Extract from its input a valid decimal number. | ||
| 533 | |||
| 534 | =cut | ||
| 535 | |||
| 536 | sub filter_decimal { | ||
| 537 | my $value = shift; | ||
| 538 | # This is a localization problem, but anyhow... | ||
| 539 | $value =~ tr/,/./; | ||
| 540 | $value =~ tr/0-9.+-//dc; | ||
| 541 | ($value) =~ m/([-+]?\d+\.?\d*)/; | ||
| 542 | return $value; | ||
| 543 | } | ||
| 544 | |||
| 545 | =pod | ||
| 546 | |||
| 547 | =item pos_decimal | ||
| 548 | |||
| 549 | Extract from its input a valid positive decimal number. | ||
| 550 | |||
| 551 | =cut | ||
| 552 | |||
| 553 | sub filter_pos_decimal { | ||
| 554 | my $value = shift; | ||
| 555 | # This is a localization problem, but anyhow... | ||
| 556 | $value =~ tr/,/./; | ||
| 557 | $value =~ tr/0-9.+//dc; | ||
| 558 | ($value) =~ m/(\+?\d+\.?\d*)/; | ||
| 559 | return $value; | ||
| 560 | } | ||
| 561 | |||
| 562 | =pod | ||
| 563 | |||
| 564 | =item neg_decimal | ||
| 565 | |||
| 566 | Extract from its input a valid negative decimal number. | ||
| 567 | |||
| 568 | =cut | ||
| 569 | |||
| 570 | sub filter_neg_decimal { | ||
| 571 | my $value = shift; | ||
| 572 | # This is a localization problem, but anyhow... | ||
| 573 | $value =~ tr/,/./; | ||
| 574 | $value =~ tr/0-9.-//dc; | ||
| 575 | ($value) =~ m/(-\d+\.?\d*)/; | ||
| 576 | return $value; | ||
| 577 | } | ||
| 578 | |||
| 579 | =pod | ||
| 580 | |||
| 581 | =item dollars | ||
| 582 | |||
| 583 | Extract from its input a valid number to express dollars like currency. | ||
| 584 | |||
| 585 | =cut | ||
| 586 | |||
| 587 | sub filter_dollars { | ||
| 588 | my $value = shift; | ||
| 589 | $value =~ tr/,/./; | ||
| 590 | $value =~ tr/0-9.+-//dc; | ||
| 591 | ($value) =~ m/(\d+\.?\d?\d?)/; | ||
| 592 | return $value; | ||
| 593 | } | ||
| 594 | |||
| 595 | =pod | ||
| 596 | |||
| 597 | =item phone | ||
| 598 | |||
| 599 | Filters out characters which aren't valid for an phone number. (Only | ||
| 600 | accept digits [0-9], space, comma, minus, parenthesis, period and pound [#].) | ||
| 601 | |||
| 602 | =cut | ||
| 603 | |||
| 604 | sub filter_phone { | ||
| 605 | my $value = shift; | ||
| 606 | $value =~ tr/0-9,().#- //dc; | ||
| 607 | return $value; | ||
| 608 | } | ||
| 609 | |||
| 610 | =pod | ||
| 611 | |||
| 612 | =item sql_wildcard | ||
| 613 | |||
| 614 | Transforms shell glob wildcard (*) to the SQL like wildcard (%). | ||
| 615 | |||
| 616 | =cut | ||
| 617 | |||
| 618 | sub filter_sql_wildcard { | ||
| 619 | my $value = shift; | ||
| 620 | $value =~ tr/*/%/; | ||
| 621 | return $value; | ||
| 622 | } | ||
| 623 | |||
| 624 | =pod | ||
| 625 | |||
| 626 | =item quotemeta | ||
| 627 | |||
| 628 | Calls the quotemeta (quote non alphanumeric character) builtin on its | ||
| 629 | input. | ||
| 630 | |||
| 631 | =cut | ||
| 632 | |||
| 633 | sub filter_quotemeta { | ||
| 634 | quotemeta $_[0]; | ||
| 635 | } | ||
| 636 | |||
| 637 | =pod | ||
| 638 | |||
| 639 | =item lc | ||
| 640 | |||
| 641 | Calls the lc (convert to lowercase) builtin on its input. | ||
| 642 | |||
| 643 | =cut | ||
| 644 | |||
| 645 | sub filter_lc { | ||
| 646 | lc $_[0]; | ||
| 647 | } | ||
| 648 | |||
| 649 | =pod | ||
| 650 | |||
| 651 | =item uc | ||
| 652 | |||
| 653 | Calls the uc (convert to uppercase) builtin on its input. | ||
| 654 | |||
| 655 | =cut | ||
| 656 | |||
| 657 | sub filter_uc { | ||
| 658 | uc $_[0]; | ||
| 659 | } | ||
| 660 | |||
| 661 | =pod | ||
| 662 | |||
| 663 | =item ucfirst | ||
| 664 | |||
| 665 | Calls the ucfirst (Uppercase first letter) builtin on its input. | ||
| 666 | |||
| 667 | =cut | ||
| 668 | |||
| 669 | sub filter_ucfirst { | ||
| 670 | ucfirst $_[0]; | ||
| 671 | } | ||
| 672 | |||
| 673 | |||
| 674 | =pod | ||
| 675 | |||
| 676 | =back | ||
| 677 | |||
| 678 | =head1 BUILTIN VALIDATOR | ||
| 679 | |||
| 680 | Those are the builtin constraint that can be specified by name in the | ||
| 681 | input profiles. | ||
| 682 | |||
| 683 | =over | ||
| 684 | |||
| 685 | =item email | ||
| 686 | |||
| 687 | Checks if the email LOOKS LIKE an email address. This checks if the | ||
| 688 | input contains one @, and a two level domain name. The address portion | ||
| 689 | is checked quite liberally. For example, all those probably invalid | ||
| 690 | address would pass the test : | ||
| 691 | |||
| 692 | nobody@top.domain | ||
| 693 | %?&/$()@nowhere.net | ||
| 694 | guessme@guess.m | ||
| 695 | |||
| 696 | =cut | ||
| 697 | |||
| 698 | # Many of the following validator are taken from | ||
| 699 | # MiniVend 3.14. (http://www.minivend.com) | ||
| 700 | # Copyright 1996-1999 by Michael J. Heins <mike@heins.net> | ||
| 701 | |||
| 702 | sub valid_email { | ||
| 703 | my $email = shift; | ||
| 704 | |||
| 705 | return $email =~ /[\040-\176]+\@[-A-Za-z0-9.]+\.[A-Za-z]+/; | ||
| 706 | } | ||
| 707 | |||
| 708 | my $state = <<EOF; | ||
| 709 | AL AK AZ AR CA CO CT DE FL GA HI ID IL IN IA KS KY LA ME MD | ||
| 710 | MA MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA PR RI | ||
| 711 | SC SD TN TX UT VT VA WA WV WI WY DC AP FP FPO APO GU VI | ||
| 712 | EOF | ||
| 713 | |||
| 714 | my $province = <<EOF; | ||
| 715 | AB BC MB NB NF NS NT ON PE QC SK YT YK | ||
| 716 | EOF | ||
| 717 | |||
| 718 | =pod | ||
| 719 | |||
| 720 | =item state_or_province | ||
| 721 | |||
| 722 | This one checks if the input correspond to an american state or a canadian | ||
| 723 | province. | ||
| 724 | |||
| 725 | =cut | ||
| 726 | |||
| 727 | sub valid_state_or_province { | ||
| 728 | return valid_state(@_) || valid_province(@_); | ||
| 729 | } | ||
| 730 | |||
| 731 | =pod | ||
| 732 | |||
| 733 | =item state | ||
| 734 | |||
| 735 | This one checks if the input is a valid two letter abbreviation of an | ||
| 736 | american state. | ||
| 737 | |||
| 738 | =cut | ||
| 739 | |||
| 740 | sub valid_state { | ||
| 741 | my $val = shift; | ||
| 742 | return $state =~ /\b$val\b/i; | ||
| 743 | } | ||
| 744 | |||
| 745 | =pod | ||
| 746 | |||
| 747 | =item province | ||
| 748 | |||
| 749 | This checks if the input is a two letter canadian province | ||
| 750 | abbreviation. | ||
| 751 | |||
| 752 | =cut | ||
| 753 | |||
| 754 | sub valid_province { | ||
| 755 | my $val = shift; | ||
| 756 | return $province =~ /\b$val\b/i; | ||
| 757 | } | ||
| 758 | |||
| 759 | =pod | ||
| 760 | |||
| 761 | =item zip_or_postcode | ||
| 762 | |||
| 763 | This constraints checks if the input is an american zipcode or a | ||
| 764 | canadian postal code. | ||
| 765 | |||
| 766 | =cut | ||
| 767 | |||
| 768 | sub valid_zip_or_postcode { | ||
| 769 | return valid_zip(@_) || valid_postcode(@_); | ||
| 770 | } | ||
| 771 | |||
| 772 | =pod | ||
| 773 | |||
| 774 | =item postcode | ||
| 775 | |||
| 776 | This constraints checks if the input is a valid Canadian postal code. | ||
| 777 | |||
| 778 | =cut | ||
| 779 | |||
| 780 | sub valid_postcode { | ||
| 781 | my $val = shift; | ||
| 782 | $val =~ s/[_\W]+//g; | ||
| 783 | return $val =~ /^[ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy]\d[A-Za-z][- ]?\d[A-Za-z]\d$/; | ||
| 784 | } | ||
| 785 | |||
| 786 | =pod | ||
| 787 | |||
| 788 | =item zip | ||
| 789 | |||
| 790 | This input validator checks if the input is a valid american zipcode : | ||
| 791 | 5 digits followed by an optional mailbox number. | ||
| 792 | |||
| 793 | =cut | ||
| 794 | |||
| 795 | sub valid_zip { | ||
| 796 | my $val = shift; | ||
| 797 | return $val =~ /^\s*\d{5}(?:[-]\d{4})?\s*$/; | ||
| 798 | } | ||
| 799 | |||
| 800 | =pod | ||
| 801 | |||
| 802 | =item phone | ||
| 803 | |||
| 804 | This one checks if the input looks like a phone number, (if it | ||
| 805 | contains at least 6 digits.) | ||
| 806 | |||
| 807 | =cut | ||
| 808 | |||
| 809 | sub valid_phone { | ||
| 810 | my $val = shift; | ||
| 811 | |||
| 812 | return $val =~ tr/0-9// >= 6; | ||
| 813 | } | ||
| 814 | |||
| 815 | =pod | ||
| 816 | |||
| 817 | =item american_phone | ||
| 818 | |||
| 819 | This constraints checks if the number is a possible North American style | ||
| 820 | of phone number : (XXX) XXX-XXXX. It has to contains more than 7 digits. | ||
| 821 | |||
| 822 | =cut | ||
| 823 | |||
| 824 | sub valid_american_phone { | ||
| 825 | my $val = shift; | ||
| 826 | return $val =~ tr/0-9// >= 7; | ||
| 827 | } | ||
| 828 | |||
| 829 | =pod | ||
| 830 | |||
| 831 | =item cc_number | ||
| 832 | |||
| 833 | This is takes two parameters, the credit card number and the credit cart | ||
| 834 | type. You should take the hash reference option for using that constraint. | ||
| 835 | |||
| 836 | The number is checked only for plausibility, it checks if the number could | ||
| 837 | be valid for a type of card by checking the checksum and looking at the number | ||
| 838 | of digits and the number of digits of the number. | ||
| 839 | |||
| 840 | This functions is only good at weeding typos and such. IT DOESN'T | ||
| 841 | CHECK IF THERE IS AN ACCOUNT ASSOCIATED WITH THE NUMBER. | ||
| 842 | |||
| 843 | =cut | ||
| 844 | |||
| 845 | # This one is taken from the contributed program to | ||
| 846 | # MiniVend by Bruce Albrecht | ||
| 847 | sub valid_cc_number { | ||
| 848 | my ( $the_card, $card_type ) = @_; | ||
| 849 | |||
| 850 | my ($index, $digit, $product); | ||
| 851 | my $multiplier = 2; # multiplier is either 1 or 2 | ||
| 852 | my $the_sum = 0; | ||
| 853 | |||
| 854 | return 0 if length($the_card) == 0; | ||
| 855 | |||
| 856 | # check card type | ||
| 857 | return 0 unless $card_type =~ /^[admv]/i; | ||
| 858 | |||
| 859 | return 0 if ($card_type =~ /^v/i && substr($the_card, 0, 1) ne "4") || | ||
| 860 | ($card_type =~ /^m/i && substr($the_card, 0, 1) ne "5") || | ||
| 861 | ($card_type =~ /^d/i && substr($the_card, 0, 4) ne "6011") || | ||
| 862 | ($card_type =~ /^a/i && substr($the_card, 0, 2) ne "34" && | ||
| 863 | substr($the_card, 0, 2) ne "37"); | ||
| 864 | |||
| 865 | # check for valid number of digits. | ||
| 866 | $the_card =~ s/\s//g; # strip out spaces | ||
| 867 | return 0 if $the_card !~ /^\d+$/; | ||
| 868 | |||
| 869 | $digit = substr($the_card, 0, 1); | ||
| 870 | $index = length($the_card)-1; | ||
| 871 | return 0 if ($digit == 3 && $index != 14) || | ||
| 872 | ($digit == 4 && $index != 12 && $index != 15) || | ||
| 873 | ($digit == 5 && $index != 15) || | ||
| 874 | ($digit == 6 && $index != 13 && $index != 15); | ||
| 875 | |||
| 876 | |||
| 877 | # calculate checksum. | ||
| 878 | for ($index--; $index >= 0; $index --) | ||
| 879 | { | ||
| 880 | $digit=substr($the_card, $index, 1); | ||
| 881 | $product = $multiplier * $digit; | ||
| 882 | $the_sum += $product > 9 ? $product - 9 : $product; | ||
| 883 | $multiplier = 3 - $multiplier; | ||
| 884 | } | ||
| 885 | $the_sum %= 10; | ||
| 886 | $the_sum = 10 - $the_sum if $the_sum; | ||
| 887 | |||
| 888 | # return whether checksum matched. | ||
| 889 | $the_sum == substr($the_card, -1); | ||
| 890 | } | ||
| 891 | |||
| 892 | =pod | ||
| 893 | |||
| 894 | =item cc_exp | ||
| 895 | |||
| 896 | This one checks if the input is in the format MM/YY or MM/YYYY and if | ||
| 897 | the MM part is a valid month (1-12) and if that date is not in the past. | ||
| 898 | |||
| 899 | =cut | ||
| 900 | |||
| 901 | sub valid_cc_exp { | ||
| 902 | my $val = shift; | ||
| 903 | |||
| 904 | my ($month, $year) = split('/', $val); | ||
| 905 | return 0 if $month !~ /^\d+$/ || $year !~ /^\d+$/; | ||
| 906 | return 0 if $month <1 || $month > 12; | ||
| 907 | $year += ($year < 70) ? 2000 : 1900 if $year < 1900; | ||
| 908 | my @now=localtime(); | ||
| 909 | $now[5] += 1900; | ||
| 910 | return 0 if ($year < $now[5]) || ($year == $now[5] && $month <= $now[4]); | ||
| 911 | |||
| 912 | return 1; | ||
| 913 | } | ||
| 914 | |||
| 915 | =pod | ||
| 916 | |||
| 917 | =item cc_type | ||
| 918 | |||
| 919 | This one checks if the input field starts by M(asterCard), V(isa), | ||
| 920 | A(merican express) or D(iscovery). | ||
| 921 | |||
| 922 | =cut | ||
| 923 | |||
| 924 | sub valid_cc_type { | ||
| 925 | my $val = shift; | ||
| 926 | |||
| 927 | return $val =~ /^[MVAD]/i; | ||
| 928 | } | ||
| 929 | |||
| 930 | 1; | ||
| 931 | |||
| 932 | __END__ |