| 1 | #!/usr/bin/perl -w | ||
| 2 | |||
| 3 | # | ||
| 4 | # | ||
| 5 | # FormMagick (c) 2000-2001 Kirrily Robert <skud@cpan.org> | ||
| 6 | # Copyright (c) 2000-2002 Mitel Networks Corporation | ||
| 7 | # This software is distributed under the same licenses as Perl itself; | ||
| 8 | # see the file COPYING for details. | ||
| 9 | # | ||
| 10 | # $Id: Basic.pm,v 1.8 2003/02/05 17:18:38 anoncvs_gtkglext Exp $ | ||
| 11 | # | ||
| 12 | |||
| 13 | package CGI::FormMagick::Validator; | ||
| 14 | use strict; | ||
| 15 | |||
| 16 | =pod | ||
| 17 | |||
| 18 | =head1 NAME | ||
| 19 | |||
| 20 | CGI::FormMagick::Validator::Basic - basic validation routines for FM | ||
| 21 | |||
| 22 | =head1 SYNOPSIS | ||
| 23 | |||
| 24 | use CGI::FormMagick; | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | See CGI::FormMagick::Validator for a general description. | ||
| 29 | |||
| 30 | =head2 Validation routines provided: | ||
| 31 | |||
| 32 | =over 4 | ||
| 33 | |||
| 34 | =item nonblank | ||
| 35 | |||
| 36 | The data is not an empty string : C<$data ne ""> | ||
| 37 | |||
| 38 | =for testing | ||
| 39 | BEGIN: { | ||
| 40 | use CGI::FormMagick::Validator; | ||
| 41 | } | ||
| 42 | is( nonblank(undef, "abc"), "OK" , "Strings aren't blank" ); | ||
| 43 | isnt( nonblank(undef, ""), "OK" , "Empty string is blank"); | ||
| 44 | isnt( nonblank(undef, " "), "OK" , "Spaces are blank"); | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | sub nonblank { | ||
| 49 | my ($fm, $data) = @_; | ||
| 50 | if (not $data) { | ||
| 51 | return "This field must not be left blank"; | ||
| 52 | } elsif ( $data =~ /^\s+$/ ) { | ||
| 53 | return "This field must not be left blank"; | ||
| 54 | } else { | ||
| 55 | return "OK"; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | =pod | ||
| 60 | |||
| 61 | =item integer | ||
| 62 | |||
| 63 | The data is a positive integer. | ||
| 64 | |||
| 65 | =for testing | ||
| 66 | is( integer(undef, 5), "OK" , "5 is a positive integer"); | ||
| 67 | isnt( integer(undef, -5), "OK" , "-5 is not a positive integer"); | ||
| 68 | isnt( integer(undef, 5.5), "OK" , "5.5 is not a positive integer"); | ||
| 69 | isnt( integer(undef, undef), "OK" , "undef is not a positive integer"); | ||
| 70 | isnt( integer(undef, "abc"), "OK" , "Alpha chars are not a positive integer"); | ||
| 71 | isnt( integer(undef, "2abc"), "OK" , "Alpha chars are not a positive integer"); | ||
| 72 | isnt( integer(undef, "2abc"), "OK" , "Mixed alphanumeric is not a positive integer"); | ||
| 73 | isnt( integer(undef, 0), "OK" , "0 is not a positive integer"); | ||
| 74 | |||
| 75 | =cut | ||
| 76 | |||
| 77 | sub integer { | ||
| 78 | my ($fm, $data) = @_; | ||
| 79 | |||
| 80 | unless ($data) { | ||
| 81 | return "This field must contain a positive integer"; | ||
| 82 | } | ||
| 83 | if ($data =~ /^[0-9]+$/) { | ||
| 84 | return "OK"; | ||
| 85 | } else { | ||
| 86 | return "This field must contain a positive integer"; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | =pod | ||
| 91 | |||
| 92 | =item number | ||
| 93 | |||
| 94 | The data is a number (positive and negative real numbers, and scientific | ||
| 95 | notation are OK). | ||
| 96 | |||
| 97 | =for testing | ||
| 98 | is( number(undef, 2), "OK" , "Integers are numbers"); | ||
| 99 | is( number(undef, 2.2), "OK" , "Real numbers are numbers"); | ||
| 100 | is( number(undef, ".2"), "OK" , "Decimal numbers with no integer part are numbers"); | ||
| 101 | isnt( number(), "OK" , "Undef is not a number"); | ||
| 102 | isnt( number(undef, "abc"), "OK" , "Alpha chars are not numbers"); | ||
| 103 | isnt( number(undef, "2abc"), "OK" , "Mixed alphanumeric is not a number"); | ||
| 104 | is( number(undef, -2), "OK" , "Negative integers are numbers"); | ||
| 105 | is( number(undef, -2.2), "OK" , "Negative real numbers are numbers"); | ||
| 106 | is( number(undef, 5.3e10), "OK" , "Scientific notation is a number"); | ||
| 107 | is( number(undef, 0), "OK" , "Zero is a number"); | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | sub number { | ||
| 112 | my ($fm, $data) = @_; | ||
| 113 | defined($data) or return "This field must contain a number"; | ||
| 114 | if ($data =~ /^-?[0-9.]+$/) { | ||
| 115 | return "OK"; | ||
| 116 | } else { | ||
| 117 | return "This field must contain a number"; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | =pod | ||
| 122 | |||
| 123 | =item word | ||
| 124 | |||
| 125 | The data looks like a single word: C<$data !~ /\W/> | ||
| 126 | |||
| 127 | =for testing | ||
| 128 | is( word(undef, "abc"), "OK" , "Alpha string is a word"); | ||
| 129 | is( word(undef, "123abc"), "OK" , "Alphanumeric string is a word"); | ||
| 130 | isnt( word(undef, ""), "OK" , "Empty string is not a word"); | ||
| 131 | isnt( word(undef, "abc def"), "OK" , "String with spaces is not a word"); | ||
| 132 | isnt( word(undef, "abc&fed"), "OK" , "String with punctuation is not a word"); | ||
| 133 | |||
| 134 | =cut | ||
| 135 | |||
| 136 | sub word { | ||
| 137 | my ($fm, $data) = @_; | ||
| 138 | if ($data =~ /^\w+$/) { | ||
| 139 | return "OK"; | ||
| 140 | } else { | ||
| 141 | return "This field must look like a single word."; | ||
| 142 | } | ||
| 143 | |||
| 144 | } | ||
| 145 | |||
| 146 | =item date | ||
| 147 | |||
| 148 | The data looks like a date. Requires the Time::ParseDate module to be | ||
| 149 | installed. | ||
| 150 | |||
| 151 | =for testing | ||
| 152 | is(date(undef, '01/01/2000'), 'OK', "date('01/01/2000') should be valid."); | ||
| 153 | is(date(undef, '01/01/00'), 'OK', "date('01/01/00') should be valid."); | ||
| 154 | is(date(undef, '1/01/00'), 'OK', "date('1/01/00') should be valid."); | ||
| 155 | is(date(undef, '1/1/00'), 'OK', "date('1/1/00') should be valid."); | ||
| 156 | is(date(undef, '12/12/00'), 'OK', "date('12/12/00') should be valid."); | ||
| 157 | is(date(undef, '12/00'), 'OK', "date('12/00') should be valid."); | ||
| 158 | is(date(undef, '12/30/00'), 'OK', "date('12/30/00') should be valid."); | ||
| 159 | is(date(undef, '30/12/00'), 'OK', "date('30/12/00') should be valid."); | ||
| 160 | isnt(date(undef, '/00'), 'OK', "date('/00') shouldn't be valid."); | ||
| 161 | isnt(date(undef, undef), 'OK', "date('undef') shouldn't be valid."); | ||
| 162 | isnt(date(undef, ''), 'OK', "date('') shouldn't be valid."); | ||
| 163 | isnt(date(undef, 'abc'), 'OK', "date('abc') shouldn't be valid."); | ||
| 164 | |||
| 165 | =cut | ||
| 166 | |||
| 167 | sub date { | ||
| 168 | my ($fm, $date) = @_; | ||
| 169 | require Time::ParseDate; | ||
| 170 | if ($date && Time::ParseDate::parsedate($date)) { | ||
| 171 | return "OK"; | ||
| 172 | } else { | ||
| 173 | return "The data entered could not be parsed as a date" | ||
| 174 | } | ||
| 175 | } | ||
| 176 | |||
| 177 | return "FALSE"; |