cztmonk has asked for the wisdom of the Perl Monks concerning the following question:

I live in the Netherlands and want to validate if a postal code (postcode) is valid. Here's the code I came up with:

#!/usr/bin/perl # Regular expression to check the postcode (NL) use strict; use warnings; while (<DATA>) { chomp; my $postcode =$_; if ($postcode =~ m{^[0-9]{4,4}\s[A-Z]{2,2}}) { print "postcode $postcode ok!\n"; } else { print "postcode $postcode not ok!\n"; } } exit 0; __DATA__ 1001 AA 1001 aa 1001AA 1001aa 100G AA 1001 Aa -100 AA

Replies are listed 'Best First'.
Re: How do I validate a postal code?
by DrHyde (Prior) on Jul 11, 2012 at 10:48 UTC
Re: How do I validate a postal code?
by Anonymous Monk on Jul 11, 2012 at 08:50 UTC
Re: How do I validate a postal code?
by cztmonk (Monk) on Jul 11, 2012 at 07:55 UTC
    #!/usr/bin/perl # Regular expression to check the postcode (NL) use strict; use warnings; while (<DATA>) { chomp; my $postcode =$_; if ($postcode =~ m{^[0-9]{4,4}\s[A-Z]{2,2}}) { print "postcode $postcode ok!\n"; } else { print "postcode $postcode not ok!\n"; } } exit 0; __DATA__ 1001 AA 1001 aa 1001AA 1001aa 100G AA 1001 Aa -100 AA

    Originally posted as a Categorized Answer.