LSTANCZYK
9/25/2017 - 1:31 AM

Custom data annotation validation attribute to validate that data is a two character U.S. state abbreviation.

Custom data annotation validation attribute to validate that data is a two character U.S. state abbreviation.

using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;


sealed public class UsStateTwoCharacterAbbreviationAttribute : ValidationAttribute
{

	private readonly string[] validStateAbbr = new string[] { "AK", "AL", "AR", "AZ", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "HI", "IA", "ID", "IL", "IN", "KS", "KY", "LA", "MA", "MD", "ME", "MI", "MN", "MO", "MS", "MT", "NC", "ND", "NE", "NH", "NJ", "NM", "NV", "NY", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VA", "VT", "WA", "WI", "WV", "WY" };

	public UsStateTwoCharacterAbbreviationAttribute() 
		: base("must be a two character U.S. state abbreviation")
	{

	}



	public override string FormatErrorMessage(string name)
	{
		return String.Format("{0} {1}", name, ErrorMessageString);
	}


	public override bool IsValid(object value)
	{
		bool isValid = false;

		if(!String.IsNullOrWhiteSpace(value.ToString()))
		{

			if(value.ToString().Length == 2 && this.validStateAbbr.Contains(value.ToString()))
			{
				isValid = true;
			}


		}

		return isValid;
	}
	

}