/* Validates US States and/or Territories by @jdforsythe
* Can be case insensitive or require capitalization - default is case insensitive
* Can include US Territories or not - default does not
* Can include US Military postal abbreviations (AA, AE, AP) - default does not
*
* Note: "States" always includes DC (District of Colombia)
*
* Usage examples:
*
* This is the default - case insensitive, no territories, no military zones
* stateInput: {
* caseSensitive: false,
* includeTerritories: false,
* includeMilitary: false
* }
*
* Only allow capital letters, no territories, no military zones
* stateInput: {
* caseSensitive: false
* }
*
* Case insensitive, include territories but not military zones
* stateInput: {
* includeTerritories: true
* }
*
* Only allow capital letters, include territories and military zones
* stateInput: {
* caseSensitive: true,
* includeTerritories: true,
* includeMilitary: true
* }
*
*/
$.validator.addMethod( "stateUS", function( value, element, options ) {
var isDefault = typeof options === "undefined",
caseSensitive = ( isDefault || typeof options.caseSensitive === "undefined" ) ? false : options.caseSensitive,
includeTerritories = ( isDefault || typeof options.includeTerritories === "undefined" ) ? false : options.includeTerritories,
includeMilitary = ( isDefault || typeof options.includeMilitary === "undefined" ) ? false : options.includeMilitary,
regex;
if ( !includeTerritories && !includeMilitary ) {
regex = "^(A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|PA|RI|S[CD]|T[NX]|UT|V[AT]|W[AIVY])$";
} else if ( includeTerritories && includeMilitary ) {
regex = "^(A[AEKLPRSZ]|C[AOT]|D[CE]|FL|G[AU]|HI|I[ADLN]|K[SY]|LA|M[ADEINOPST]|N[CDEHJMVY]|O[HKR]|P[AR]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY])$";
} else if ( includeTerritories ) {
regex = "^(A[KLRSZ]|C[AOT]|D[CE]|FL|G[AU]|HI|I[ADLN]|K[SY]|LA|M[ADEINOPST]|N[CDEHJMVY]|O[HKR]|P[AR]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY])$";
} else {
regex = "^(A[AEKLPRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|PA|RI|S[CD]|T[NX]|UT|V[AT]|W[AIVY])$";
}
regex = caseSensitive ? new RegExp( regex ) : new RegExp( regex, "i" );
return this.optional( element ) || regex.test( value );
}, "Please specify a valid state" );
|