package CGI::Untaint::CountyStateProvince::US;

use warnings;
use strict;
use Locale::SubCountry;

# use base qw(CGI::Untaint::object CGI::Untaint::CountyStateProvince);
use base 'CGI::Untaint::object';

=head1 NAME

CGI::Untaint::CountyStateProvince::US - Add U.S. states to CGI::Untaint::CountyStateProvince tables

=head1 VERSION

Version 0.04


our $VERSION = '0.04';


Adds a list of U.S. states to the list of counties/state/provinces
which are known by the CGI::Untaint::CountyStateProvince validator allowing you
to verify that a field in an HTML form contains a valid U.S. state.

You must include CGI::Untaint::CountyStateProvince::US after including
CGI::Untaint, otherwise it won't work.

    use CGI::Info;
    use CGI::Untaint;
    use CGI::Untaint::CountyStateProvince::US;
    my $info = CGI::Info->new();
    my $u = CGI::Untaint->new($info->params());
    # Succeeds if state = 'MD' or 'Maryland', fails if state = 'Queensland';
    $u->extract(-as_CountyStateProvince => 'state');
    # ...



=head2 is_valid

Validates the data, setting the data to be the two letter abbreviation for the
given state.  See CGI::Untaint::is_valid.


sub is_valid {
	my $self = shift;

	my $value = uc($self->value);

	if($value =~ /([A-Z][A-Z\s]+)/) {
		$value = $1;
	} else {
		return 0;

	unless($self->{_validator}) {
		$self->{_validator} = Locale::SubCountry->new('US');
		unless($self->{_validator}) {
			return 0;

	my $state = $self->{_validator}->code($value);
	if($state && ($state ne 'unknown')) {
		# Given full state name
		# Detaintify
		if($state =~ /(^[A-Z]{2}$)/) {
			return $1;

	$state = $self->{_validator}->full_name($value);
	if($state && ($state ne 'unknown')) {
		# Given two letter abbreviation
		return $value;

	return 0;

=head2 value

Sets the raw data which is to be validated.  Called by the superclass, you
are unlikely to want to call it.


sub value {
	my ($self, $value) = @_;

	if(defined($value)) {
		$self->{value} = $value;

	return $self->{value};

	my $us = CGI::Untaint::CountyStateProvince::US->_new();

	push @CGI::Untaint::CountyStateProvince::countries, $us;

=head1 AUTHOR

Nigel Horne, C<< <njh at> >>

=head1 BUGS

Only two letter abbreviations are allowable, so 'Mass' won't work for

Please report any bugs or feature requests to C<bug-cgi-untaint-csp-us at>, or through
the web interface at L<>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.

=head1 SEE ALSO

CGI::Untaint::CountyStateProvince, CGI::Untaint

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc CGI::Untaint::CountyStateProvince::US

You can also look for information at:

=over 4

=item * RT: CPAN's request tracker


=item * CPAN Ratings


=item * Search CPAN





Copyright 2012-2019 Nigel Horne.

This program is released under the following licence: GPL2


1; # End of CGI::Untaint::CountyStateProvince::US