| 1 | #!/usr/bin/perl -w | ||
| 2 | |||
| 3 | # | ||
| 4 | # FormMagick (c) 2000-2001 Kirrily Robert <skud@cpan.org> | ||
| 5 | # Copyright (c) 2000-2002 Mitel Networks Corporation | ||
| 6 | # This software is distributed under the same licenses as Perl itself; | ||
| 7 | # see the file COPYING for details. | ||
| 8 | |||
| 9 | # | ||
| 10 | # $Id: Length.pm,v 1.6 2003/02/05 17:18:40 anoncvs_gtkglext Exp $ | ||
| 11 | # | ||
| 12 | |||
| 13 | package CGI::FormMagick::Validator; | ||
| 14 | |||
| 15 | =pod | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | CGI::FormMagick::Validator::Length - length-related validation routines | ||
| 20 | |||
| 21 | =head1 VALIDATION ROUTINES | ||
| 22 | |||
| 23 | =item minlength(n) | ||
| 24 | |||
| 25 | The data is at least C<n> characters long: C<length($data) E<gt>= $n> | ||
| 26 | |||
| 27 | =begin testing | ||
| 28 | BEGIN: { | ||
| 29 | use CGI::FormMagick::Validator; | ||
| 30 | } | ||
| 31 | |||
| 32 | is( minlength(undef, "abc", 2), "OK" , "3 letter string is at least 2 chars long"); | ||
| 33 | isnt( minlength(undef, "abc", -2), "OK" , "Negative minlength should fail"); | ||
| 34 | isnt( minlength(undef, "abc", 0), "OK" , "Zero minlength should fail"); | ||
| 35 | isnt( minlength(undef, "abc", "def"), "OK" , "Non-numeric minlength should fail"); | ||
| 36 | isnt( minlength(undef, "", 1), "OK" , "Too short string should fail"); | ||
| 37 | |||
| 38 | =end testing | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | sub minlength { | ||
| 43 | my ($fm, $data, $minlength) = @_; | ||
| 44 | if (number($fm, $minlength) ne "OK" or $minlength <= 0) { | ||
| 45 | return "Minimum length has been specified meaninglessly as $minlength"; | ||
| 46 | } | ||
| 47 | if ( length($data) >= $minlength ) { | ||
| 48 | return "OK"; | ||
| 49 | } else { | ||
| 50 | return "This field must be at least $minlength characters"; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | =pod | ||
| 56 | |||
| 57 | =item maxlength(n) | ||
| 58 | |||
| 59 | The data is no more than C<n> characters long: C<length($data) E<lt>= $n> | ||
| 60 | |||
| 61 | =for testing | ||
| 62 | is( maxlength(undef, "abc", 5), "OK" , "3 letter string is less than 5 chars long"); | ||
| 63 | isnt( maxlength(undef, "abc", -2), "OK" , "Negative maxlength should fail"); | ||
| 64 | isnt( maxlength(undef, "abc", 0), "OK" , "Zero maxlength should fail"); | ||
| 65 | isnt( maxlength(undef, "abc", "def"), "OK" , "Non-numeric maxlength should fail"); | ||
| 66 | is( maxlength(undef, "", 1), "OK" , "Zero length string is less than 1 char long"); | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | sub maxlength { | ||
| 71 | my ($fm, $data, $maxlength) = @_; | ||
| 72 | if (number($fm, $maxlength) ne "OK" or $maxlength <= 0) { | ||
| 73 | return "Maximum length has been specified meaninglessly as $maxlength"; | ||
| 74 | } | ||
| 75 | if ( length($data) <= $maxlength ) { | ||
| 76 | return "OK"; | ||
| 77 | } else { | ||
| 78 | return "This field must be no more than $maxlength characters"; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | =pod | ||
| 83 | |||
| 84 | =item exactlength(n) | ||
| 85 | |||
| 86 | The data is exactly C<n> characters long: C<length($data) E== $n> | ||
| 87 | |||
| 88 | =for testing | ||
| 89 | is( exactlength(undef, "abc", 3), "OK" , "3 letter string is 3 chars long"); | ||
| 90 | isnt( exactlength(undef, "abc", 5), "OK" , "3 letter string isn't 5 chars long"); | ||
| 91 | isnt( exactlength(undef, "abc", -2), "OK" , "Negative length should fail"); | ||
| 92 | is( exactlength(undef, "", 0), "OK" , "Empty string is zero length"); | ||
| 93 | isnt( exactlength(undef, "abc", "def"), "OK" , "Non-numeric exactlength should fail"); | ||
| 94 | isnt( exactlength(undef, "abc"), "OK", "undef exactlength should fail"); | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | sub exactlength { | ||
| 99 | my ($fm, $data, $exactlength) = @_; | ||
| 100 | if (not defined $exactlength) { | ||
| 101 | return "You must specify the length for the field."; | ||
| 102 | } elsif ( $exactlength =~ /\D/ ) { | ||
| 103 | return "You must specify the exactlength of the field with an integer"; | ||
| 104 | } elsif ( length($data) == $exactlength ) { | ||
| 105 | return "OK"; | ||
| 106 | } else { | ||
| 107 | return "This field must be exactly $exactlength characters"; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | |||
| 112 | =pod | ||
| 113 | |||
| 114 | =item lengthrange(n,m) | ||
| 115 | |||
| 116 | The data is between C<n> and c<m> characters long: C<length($data) E<gt>= $n> | ||
| 117 | and C<length($data) E<lt>= $m>. | ||
| 118 | |||
| 119 | =for testing | ||
| 120 | ok( CGI::FormMagick::Validator->can('lengthrange'), "Lengthrange routine exists"); | ||
| 121 | is( lengthrange(undef, "abc", 2,4), "OK" , "3 letter string is between 2 and 4 chars long"); | ||
| 122 | is( lengthrange(undef, "abc", 3,3), "OK" , "3 letter string is between 3 and 3 chars long"); | ||
| 123 | isnt( lengthrange(undef, "abc", 1,2), "OK" , "3 letter string is not between 1 and 2 chars long"); | ||
| 124 | is( lengthrange(undef, "", 0,1), "OK" , "Empty string is zero length"); | ||
| 125 | isnt( lengthrange(undef, "abc", -2,4), "OK" , "Negative length should fail"); | ||
| 126 | isnt( lengthrange(undef, "abc", 5,3), "OK" , "Max length is less than min length"); | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | sub lengthrange { | ||
| 131 | my ($fm, $data, $minlength, $maxlength) = @_; | ||
| 132 | if (not defined $minlength or not defined $maxlength) { | ||
| 133 | return "You must specify the maximum and minimum length for the field."; | ||
| 134 | } elsif ( $maxlength =~ /\D/ or $minlength =~ /\D/ ) { | ||
| 135 | return "You must specify the maximum and minimum lengths of the field with an integer"; | ||
| 136 | } elsif ( ( length($data) >= $minlength ) and (length($data) <= $maxlength) ) { | ||
| 137 | return "OK"; | ||
| 138 | } else { | ||
| 139 | return "This field must be between $minlength and $maxlength characters"; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | |||
| 144 | =pod | ||
| 145 | |||
| 146 | =head1 SEE ALSO | ||
| 147 | |||
| 148 | The main perldoc for CGI::FormMagick | ||
| 149 | |||
| 150 | =head1 AUTHOR | ||
| 151 | |||
| 152 | Kirrily "Skud" Robert <skud@infotrope.net> | ||
| 153 | |||
| 154 | More information about FormMagick may be found at | ||
| 155 | http://sourceforge.net/projects/formmagick/ | ||
| 156 | |||
| 157 | =cut | ||
| 158 | |||
| 159 | return 1; |