| 1 | # | ||
| 2 | # Filters.pm - Common filters for use in HTML::FormValidator. | ||
| 3 | # | ||
| 4 | # This file is part of FormValidator. | ||
| 5 | # | ||
| 6 | # Author: Francis J. Lacoste <francis.lacoste@iNsu.COM> | ||
| 7 | # | ||
| 8 | # Copyright (C) 1999,2000 iNsu Innovations Inc. | ||
| 9 | # | ||
| 10 | # This program is free software; you can redistribute it and/or modify | ||
| 11 | # it under the terms same terms as perl itself. | ||
| 12 | # | ||
| 13 | use strict; | ||
| 14 | |||
| 15 | package HTML::FormValidator::Filters; | ||
| 16 | |||
| 17 | =pod | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | HTML::FormValidator::Filters - Basic set of filters available in an HTML::FormValidor profile. | ||
| 22 | |||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | In an HTML::Empberl page: | ||
| 27 | |||
| 28 | use HTML::FormValidator; | ||
| 29 | |||
| 30 | my $validator = new HTML::FormValidator( "/home/user/input_profiles.pl" ); | ||
| 31 | my ( $valid, $missing, $invalid, $unknown ) = $validator->validate( \%fdat, "customer_infos" ); | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | These are the builtin filters which may be specified as name in the | ||
| 36 | I<filters> and I<field_filters> parameters of the input profile. | ||
| 37 | |||
| 38 | =over | ||
| 39 | |||
| 40 | =item trim | ||
| 41 | |||
| 42 | Remove white space at the front and end of the fields. | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | sub filter_trim { | ||
| 47 | my $value = shift; | ||
| 48 | |||
| 49 | # Remove whitespace at the front | ||
| 50 | $value =~ s/^\s+//g; | ||
| 51 | |||
| 52 | # Remove whitespace at the end | ||
| 53 | $value =~ s/\s+$//g; | ||
| 54 | |||
| 55 | return $value; | ||
| 56 | } | ||
| 57 | |||
| 58 | =pod | ||
| 59 | |||
| 60 | =item strip | ||
| 61 | |||
| 62 | Runs of white space are replaced by a single space. | ||
| 63 | |||
| 64 | =cut | ||
| 65 | |||
| 66 | sub filter_strip { | ||
| 67 | my $value = shift; | ||
| 68 | |||
| 69 | # Strip whitespace | ||
| 70 | $value =~ s/\s+/ /g; | ||
| 71 | |||
| 72 | return $value; | ||
| 73 | } | ||
| 74 | |||
| 75 | =pod | ||
| 76 | |||
| 77 | =item digit | ||
| 78 | |||
| 79 | Remove non digits characters from the input. | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | sub filter_digit { | ||
| 84 | my $value = shift; | ||
| 85 | $value =~ s/\D//g; | ||
| 86 | |||
| 87 | return $value; | ||
| 88 | } | ||
| 89 | |||
| 90 | =pod | ||
| 91 | |||
| 92 | =item alphanum | ||
| 93 | |||
| 94 | Remove non alphanumerical characters from the input. | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | sub filter_alphanum { | ||
| 99 | my $value = shift; | ||
| 100 | $value =~ s/\W//g; | ||
| 101 | return $value; | ||
| 102 | } | ||
| 103 | |||
| 104 | =pod | ||
| 105 | |||
| 106 | =item integer | ||
| 107 | |||
| 108 | Extract from its input a valid integer number. | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | sub filter_integer { | ||
| 113 | my $value = shift; | ||
| 114 | $value =~ tr/0-9+-//dc; | ||
| 115 | ($value) =~ m/([-+]?\d+)/; | ||
| 116 | return $value; | ||
| 117 | } | ||
| 118 | |||
| 119 | =pod | ||
| 120 | |||
| 121 | =item pos_integer | ||
| 122 | |||
| 123 | Extract from its input a valid positive integer number. | ||
| 124 | |||
| 125 | =cut | ||
| 126 | |||
| 127 | sub filter_pos_integer { | ||
| 128 | my $value = shift; | ||
| 129 | $value =~ tr/0-9+//dc; | ||
| 130 | ($value) =~ m/(\+?\d+)/; | ||
| 131 | return $value; | ||
| 132 | } | ||
| 133 | |||
| 134 | =pod | ||
| 135 | |||
| 136 | =item pos_integer | ||
| 137 | |||
| 138 | Extract from its input a valid negative integer number. | ||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | sub filter_neg_integer { | ||
| 143 | my $value = shift; | ||
| 144 | $value =~ tr/0-9-//dc; | ||
| 145 | ($value) =~ m/(-\d+)/; | ||
| 146 | return $value; | ||
| 147 | } | ||
| 148 | |||
| 149 | =pod | ||
| 150 | |||
| 151 | =item decimal | ||
| 152 | |||
| 153 | Extract from its input a valid decimal number. | ||
| 154 | |||
| 155 | =cut | ||
| 156 | |||
| 157 | sub filter_decimal { | ||
| 158 | my $value = shift; | ||
| 159 | # This is a localization problem, but anyhow... | ||
| 160 | $value =~ tr/,/./; | ||
| 161 | $value =~ tr/0-9.+-//dc; | ||
| 162 | ($value) =~ m/([-+]?\d+\.?\d*)/; | ||
| 163 | return $value; | ||
| 164 | } | ||
| 165 | |||
| 166 | =pod | ||
| 167 | |||
| 168 | =item pos_decimal | ||
| 169 | |||
| 170 | Extract from its input a valid positive decimal number. | ||
| 171 | |||
| 172 | =cut | ||
| 173 | |||
| 174 | sub filter_pos_decimal { | ||
| 175 | my $value = shift; | ||
| 176 | # This is a localization problem, but anyhow... | ||
| 177 | $value =~ tr/,/./; | ||
| 178 | $value =~ tr/0-9.+//dc; | ||
| 179 | ($value) =~ m/(\+?\d+\.?\d*)/; | ||
| 180 | return $value; | ||
| 181 | } | ||
| 182 | |||
| 183 | =pod | ||
| 184 | |||
| 185 | =item neg_decimal | ||
| 186 | |||
| 187 | Extract from its input a valid negative decimal number. | ||
| 188 | |||
| 189 | =cut | ||
| 190 | |||
| 191 | sub filter_neg_decimal { | ||
| 192 | my $value = shift; | ||
| 193 | # This is a localization problem, but anyhow... | ||
| 194 | $value =~ tr/,/./; | ||
| 195 | $value =~ tr/0-9.-//dc; | ||
| 196 | ($value) =~ m/(-\d+\.?\d*)/; | ||
| 197 | return $value; | ||
| 198 | } | ||
| 199 | |||
| 200 | =pod | ||
| 201 | |||
| 202 | =item dollars | ||
| 203 | |||
| 204 | Extract from its input a valid number to express dollars like currency. | ||
| 205 | |||
| 206 | =cut | ||
| 207 | |||
| 208 | sub filter_dollars { | ||
| 209 | my $value = shift; | ||
| 210 | $value =~ tr/,/./; | ||
| 211 | $value =~ tr/0-9.+-//dc; | ||
| 212 | ($value) =~ m/(\d+\.?\d?\d?)/; | ||
| 213 | return $value; | ||
| 214 | } | ||
| 215 | |||
| 216 | =pod | ||
| 217 | |||
| 218 | =item phone | ||
| 219 | |||
| 220 | Filters out characters which aren't valid for an phone number. (Only | ||
| 221 | accept digits [0-9], space, comma, minus, parenthesis, period and pound [#].) | ||
| 222 | |||
| 223 | =cut | ||
| 224 | |||
| 225 | sub filter_phone { | ||
| 226 | my $value = shift; | ||
| 227 | $value =~ tr/0-9,().#- //dc; | ||
| 228 | return $value; | ||
| 229 | } | ||
| 230 | |||
| 231 | =pod | ||
| 232 | |||
| 233 | =item sql_wildcard | ||
| 234 | |||
| 235 | Transforms shell glob wildcard (*) to the SQL like wildcard (%). | ||
| 236 | |||
| 237 | =cut | ||
| 238 | |||
| 239 | sub filter_sql_wildcard { | ||
| 240 | my $value = shift; | ||
| 241 | $value =~ tr/*/%/; | ||
| 242 | return $value; | ||
| 243 | } | ||
| 244 | |||
| 245 | =pod | ||
| 246 | |||
| 247 | =item quotemeta | ||
| 248 | |||
| 249 | Calls the quotemeta (quote non alphanumeric character) builtin on its | ||
| 250 | input. | ||
| 251 | |||
| 252 | =cut | ||
| 253 | |||
| 254 | sub filter_quotemeta { | ||
| 255 | quotemeta $_[0]; | ||
| 256 | } | ||
| 257 | |||
| 258 | =pod | ||
| 259 | |||
| 260 | =item lc | ||
| 261 | |||
| 262 | Calls the lc (convert to lowercase) builtin on its input. | ||
| 263 | |||
| 264 | =cut | ||
| 265 | |||
| 266 | sub filter_lc { | ||
| 267 | lc $_[0]; | ||
| 268 | } | ||
| 269 | |||
| 270 | =pod | ||
| 271 | |||
| 272 | =item uc | ||
| 273 | |||
| 274 | Calls the uc (convert to uppercase) builtin on its input. | ||
| 275 | |||
| 276 | =cut | ||
| 277 | |||
| 278 | sub filter_uc { | ||
| 279 | uc $_[0]; | ||
| 280 | } | ||
| 281 | |||
| 282 | =pod | ||
| 283 | |||
| 284 | =item ucfirst | ||
| 285 | |||
| 286 | Calls the ucfirst (Uppercase first letter) builtin on its input. | ||
| 287 | |||
| 288 | =cut | ||
| 289 | |||
| 290 | sub filter_ucfirst { | ||
| 291 | ucfirst $_[0]; | ||
| 292 | } | ||
| 293 | |||
| 294 | 1; | ||
| 295 | __END__ |