line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTML::CheckArgs::postal_code; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
36
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
6
|
use base 'HTML::CheckArgs::Object'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
2442
|
|
7
|
1
|
|
|
1
|
|
501
|
use HTML::FormatData; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub is_valid { |
10
|
|
|
|
|
|
|
my $self = shift; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
my $value = $self->value; |
13
|
|
|
|
|
|
|
my $config = $self->config; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
$self->check_params( required => [], optional => [ 'country' ], cleanable => 1 ); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# no value passed in |
18
|
|
|
|
|
|
|
if ( $config->{required} && !$value ) { |
19
|
|
|
|
|
|
|
$self->error_code( 'postal_code_00' ); # required |
20
|
|
|
|
|
|
|
$self->error_message( 'Not given.' ); |
21
|
|
|
|
|
|
|
return; |
22
|
|
|
|
|
|
|
} elsif ( !$config->{required} && !$value ) { |
23
|
|
|
|
|
|
|
return 1; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# clean it up for validation |
27
|
|
|
|
|
|
|
# for US case, we clean it up a lot more below |
28
|
|
|
|
|
|
|
$value = HTML::FormatData->new->format_text( |
29
|
|
|
|
|
|
|
$value, clean_whitespace => 1, strip_html => 1, |
30
|
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# which country? must be a two-character country abbr |
33
|
|
|
|
|
|
|
# only does careful validation check for US for now |
34
|
|
|
|
|
|
|
my $country = $config->{params}{country} || 'US'; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
if ( uc( $country ) eq 'US' ) { |
37
|
|
|
|
|
|
|
$value =~ tr/0-9//cd; |
38
|
|
|
|
|
|
|
if ( ( $value !~ m/^\d{5}$/ ) && ( $value !~ m/^\d{9}$/ ) ) { |
39
|
|
|
|
|
|
|
$self->error_code( 'postal_code_01' ); # not valid |
40
|
|
|
|
|
|
|
$self->error_message( 'Not valid; please enter a 5 or 9 digit ZIP code.' ); |
41
|
|
|
|
|
|
|
return; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# if not US, just do a sanity check on length |
45
|
|
|
|
|
|
|
} elsif ( length( $value ) > 100 ) { |
46
|
|
|
|
|
|
|
$self->error_code( 'postal_code_02' ); # over max length |
47
|
|
|
|
|
|
|
$self->error_message( 'Exceeds the maximum allowable length (100 characters).' ); |
48
|
|
|
|
|
|
|
return; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# return cleaned value? |
52
|
|
|
|
|
|
|
unless ( $config->{noclean} ) { |
53
|
|
|
|
|
|
|
$self->value( $value ); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
return 1; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
1; |