FormValidator/Constraints.pm untested

filter: Statementstmt 0.0% 0 / 57 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 69 MC/DCmcdc 0.0% 0 / 39 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 214 CC 13 SCAR 35.3
CC
13
Cov
0%
valid_cc_number
72.0
valid_cc_exp
42.0
valid_email
2.0
CRAP 34.0
1 #
2 # Constraints.pm - Standard constraints for use in HTML::FormValidator.
3 #
4 # This file is part of FormValidator.
5 #
6 # Author: Francis J. Lacoste <francis.lacoste@Contre.COM>
7 #
8 # Copyright (C) 1999,2000 iNsu Innovations Inc.
9 # Copyright (C) 2001 Francis J. Lacoste
10 # Parts Copyright 1996-1999 by Michael J. Heins <mike@heins.net>
11 # Parts Copyright 1996-1999 by Bruce Albrecht <bruce.albrecht@seag.fingerhut.com>
12 #
13 # Parts of this module are based on work by
14 # Bruce Albrecht, <bruce.albrecht@seag.fingerhut.com> contributed to
15 # MiniVend.
16 #
17 # Parts also based on work by Michael J. Heins <mikeh@minivend.com>
18 #
19 # This program is free software; you can redistribute it and/or modify
20 # it under the terms same terms as perl itself.
21 #
22 use strict;
23  
24 package HTML::FormValidator::Constraints;
25  
26 =pod
27  
28 =pod
29  
30 =head1 NAME
31  
32 HTML::FormValidator::Constraints - Basic sets of constraints.
33 on input profile.
34  
35 =head1 SYNOPSIS
36  
37 In an HTML::FormValidator profile:
38  
39 constraints =>
40 {
41 email => "email",
42 fax => "american_phone",
43 phone => "american_phone",
44 state => "state",
45 },
46  
47 =head1 DESCRIPTION
48  
49 Those are the builtin constraint that can be specified by name in the
50 input profiles.
51  
52 =over
53  
54 =item email
55  
56 Checks if the email LOOKS LIKE an email address. This checks if the
57 input contains one @, and a two level domain name. The address portion
58 is checked quite liberally. For example, all those probably invalid
59 address would pass the test :
60  
61 nobody@top.domain
62 %?&/$()@nowhere.net
63 guessme@guess.m
64  
65 =cut
66  
67 # Many of the following validator are taken from
68 # MiniVend 3.14. (http://www.minivend.com)
69 # Copyright 1996-1999 by Michael J. Heins <mike@heins.net>
70  
71 sub valid_email {
72 my $email = shift;
73  
74 return $email =~ /[\040-\176]+\@[-A-Za-z0-9.]+\.[A-Za-z]+/;
75 }
76  
77 my $state = <<EOF;
78 AL AK AZ AR CA CO CT DE FL GA HI ID IL IN IA KS KY LA ME MD
79 MA MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA PR RI
80 SC SD TN TX UT VT VA WA WV WI WY DC AP FP FPO APO GU VI
81 EOF
82  
83 my $province = <<EOF;
84 AB BC MB NB NF NS NT ON PE QC SK YT YK NU
85 EOF
86  
87 =pod
88  
89 =item state_or_province
90  
91 This one checks if the input correspond to an american state or a canadian
92 province.
93  
94 =cut
95  
96 sub valid_state_or_province {
97 return valid_state(@_) || valid_province(@_);
98 }
99  
100 =pod
101  
102 =item state
103  
104 This one checks if the input is a valid two letter abbreviation of an
105 american state.
106  
107 =cut
108  
109 sub valid_state {
110 my $val = shift;
111 return $state =~ /\b$val\b/i;
112 }
113  
114 =pod
115  
116 =item province
117  
118 This checks if the input is a two letter canadian province
119 abbreviation.
120  
121 =cut
122  
123 sub valid_province {
124 my $val = shift;
125 return $province =~ /\b$val\b/i;
126 }
127  
128 =pod
129  
130 =item zip_or_postcode
131  
132 This constraints checks if the input is an american zipcode or a
133 canadian postal code.
134  
135 =cut
136  
137 sub valid_zip_or_postcode {
138 return valid_zip(@_) || valid_postcode(@_);
139 }
140  
141 =pod
142  
143 =item postcode
144  
145 This constraints checks if the input is a valid Canadian postal code.
146  
147 =cut
148  
149 sub valid_postcode {
150 my $val = shift;
151 $val =~ s/[_\W]+//g;
152 return $val =~ /^[ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy]\d[A-Za-z][- ]?\d[A-Za-z]\d$/;
153 }
154  
155 =pod
156  
157 =item zip
158  
159 This input validator checks if the input is a valid american zipcode :
160 5 digits followed by an optional mailbox number.
161  
162 =cut
163  
164 sub valid_zip {
165 my $val = shift;
166 return $val =~ /^\s*\d{5}(?:[-]\d{4})?\s*$/;
167 }
168  
169 =pod
170  
171 =item phone
172  
173 This one checks if the input looks like a phone number, (if it
174 contains at least 6 digits.)
175  
176 =cut
177  
178 sub valid_phone {
179 my $val = shift;
180  
181 return $val =~ tr/0-9// >= 6;
182 }
183  
184 =pod
185  
186 =item american_phone
187  
188 This constraints checks if the number is a possible North American style
189 of phone number : (XXX) XXX-XXXX. It has to contains more than 7 digits.
190  
191 =cut
192  
193 sub valid_american_phone {
194 my $val = shift;
195 return $val =~ tr/0-9// >= 7;
196 }
197  
198 =pod
199  
200 =item cc_number
201  
202 This is takes two parameters, the credit card number and the credit cart
203 type. You should take the hash reference option for using that constraint.
204  
205 The number is checked only for plausibility, it checks if the number could
206 be valid for a type of card by checking the checksum and looking at the number
207 of digits and the number of digits of the number.
208  
209 This functions is only good at weeding typos and such. IT DOESN'T
210 CHECK IF THERE IS AN ACCOUNT ASSOCIATED WITH THE NUMBER.
211  
212 =cut
213  
214 # This one is taken from the contributed program to
215 # MiniVend by Bruce Albrecht
216 sub valid_cc_number {
217 my ( $the_card, $card_type ) = @_;
218  
219 my ($index, $digit, $product);
220 my $multiplier = 2; # multiplier is either 1 or 2
221 my $the_sum = 0;
222  
223 return 0 if length($the_card) == 0;
224  
225 # check card type
226 return 0 unless $card_type =~ /^[admv]/i;
227  
228 return 0 if ($card_type =~ /^v/i && substr($the_card, 0, 1) ne "4") ||
229 ($card_type =~ /^m/i && substr($the_card, 0, 1) ne "5") ||
230 ($card_type =~ /^d/i && substr($the_card, 0, 4) ne "6011") ||
231 ($card_type =~ /^a/i && substr($the_card, 0, 2) ne "34" &&
232 substr($the_card, 0, 2) ne "37");
233  
234 # check for valid number of digits.
235 $the_card =~ s/\s//g; # strip out spaces
236 return 0 if $the_card !~ /^\d+$/;
237  
238 $digit = substr($the_card, 0, 1);
239 $index = length($the_card)-1;
240 return 0 if ($digit == 3 && $index != 14) ||
241 ($digit == 4 && $index != 12 && $index != 15) ||
242 ($digit == 5 && $index != 15) ||
243 ($digit == 6 && $index != 13 && $index != 15);
244  
245  
246 # calculate checksum.
247 for ($index--; $index >= 0; $index --)
248 {
249 $digit=substr($the_card, $index, 1);
250 $product = $multiplier * $digit;
251 $the_sum += $product > 9 ? $product - 9 : $product;
252 $multiplier = 3 - $multiplier;
253 }
254 $the_sum %= 10;
255 $the_sum = 10 - $the_sum if $the_sum;
256  
257 # return whether checksum matched.
258 $the_sum == substr($the_card, -1);
259 }
260  
261 =pod
262  
263 =item cc_exp
264  
265 This one checks if the input is in the format MM/YY or MM/YYYY and if
266 the MM part is a valid month (1-12) and if that date is not in the past.
267  
268 =cut
269  
270 sub valid_cc_exp {
271 my $val = shift;
272  
273 my ($month, $year) = split('/', $val);
274 return 0 if $month !~ /^\d+$/ || $year !~ /^\d+$/;
275 return 0 if $month <1 || $month > 12;
276 $year += ($year < 70) ? 2000 : 1900 if $year < 1900;
277 my @now=localtime();
278 $now[5] += 1900;
279 return 0 if ($year < $now[5]) || ($year == $now[5] && $month <= $now[4]);
280  
281 return 1;
282 }
283  
284 =pod
285  
286 =item cc_type
287  
288 This one checks if the input field starts by M(asterCard), V(isa),
289 A(merican express) or D(iscovery).
290  
291 =cut
292  
293 sub valid_cc_type {
294 my $val = shift;
295  
296 return $val =~ /^[MVAD]/i;
297 }
298  
299 1;
300  
301 __END__