CGI::Untaint::uk_postcode - validate a UK postcode


CGI-Untaint-uk_postcode documentation Contained in the CGI-Untaint-uk_postcode distribution.

Index


Code Index:

NAME

Top

CGI::Untaint::uk_postcode - validate a UK postcode

SYNOPSIS

Top

  use CGI::Untaint;
  my $handler = CGI::Untaint->new($q->Vars);

  my $url = $handler->extract( -as_uk_postcode => 'postcode' );

DESCRIPTION

Top

This CGI::Untaint input handler verifies that it is dealing with a reasonably plausible UK postcode, according to some checks by the Royal Mail.

Due to the complexities of the UK postal code system it is impossible to accurately check if the postcode is real, or even if it is of a completely valid format. As such there may be false positives. There should not, however, be any false negatives, so if you find any valid postcodes that this rejects, PLEASE let me know.

SEE ALSO

Top

http://en.wikipedia.org/wiki/Postcode

CGI::Untaint

AUTHOR

Top

Tony Bowden. Based on original regular expression by Craig Berry.

BUGS and QUERIES

Top

Please direct all correspondence regarding this module to: bug-CGI-Untaint-uk_postcode@rt.cpan.org

COPYRIGHT

Top


CGI-Untaint-uk_postcode documentation Contained in the CGI-Untaint-uk_postcode distribution.

package CGI::Untaint::uk_postcode;

$VERSION = '1.00';

use strict;
use base 'CGI::Untaint::object';


sub _untaint_re { 
  my @patterns = ('ON NII', 'ONN NII', 'OON NII', 'OONN NII',
                  'ONO NII', 'OONO NII', 'OOO NII');

  foreach (@patterns) {
    s/N/\\d/g;
    s/O/[A-Z]/g; # outward code
    s/I/[ABDEFGHJLNPQRSTUWXYZ]/g; # inward code
    s/ /\\s?/g;
  }

  my $re = join '|', @patterns;
  return qr/^($re)$/i;
}

1;