| 4 |
lars |
1 |
/* Validates US States and/or Territories by @jdforsythe
|
|
|
2 |
* Can be case insensitive or require capitalization - default is case insensitive
|
|
|
3 |
* Can include US Territories or not - default does not
|
|
|
4 |
* Can include US Military postal abbreviations (AA, AE, AP) - default does not
|
|
|
5 |
*
|
|
|
6 |
* Note: "States" always includes DC (District of Colombia)
|
|
|
7 |
*
|
|
|
8 |
* Usage examples:
|
|
|
9 |
*
|
|
|
10 |
* This is the default - case insensitive, no territories, no military zones
|
|
|
11 |
* stateInput: {
|
|
|
12 |
* caseSensitive: false,
|
|
|
13 |
* includeTerritories: false,
|
|
|
14 |
* includeMilitary: false
|
|
|
15 |
* }
|
|
|
16 |
*
|
|
|
17 |
* Only allow capital letters, no territories, no military zones
|
|
|
18 |
* stateInput: {
|
|
|
19 |
* caseSensitive: false
|
|
|
20 |
* }
|
|
|
21 |
*
|
|
|
22 |
* Case insensitive, include territories but not military zones
|
|
|
23 |
* stateInput: {
|
|
|
24 |
* includeTerritories: true
|
|
|
25 |
* }
|
|
|
26 |
*
|
|
|
27 |
* Only allow capital letters, include territories and military zones
|
|
|
28 |
* stateInput: {
|
|
|
29 |
* caseSensitive: true,
|
|
|
30 |
* includeTerritories: true,
|
|
|
31 |
* includeMilitary: true
|
|
|
32 |
* }
|
|
|
33 |
*
|
|
|
34 |
*/
|
|
|
35 |
$.validator.addMethod( "stateUS", function( value, element, options ) {
|
|
|
36 |
var isDefault = typeof options === "undefined",
|
|
|
37 |
caseSensitive = ( isDefault || typeof options.caseSensitive === "undefined" ) ? false : options.caseSensitive,
|
|
|
38 |
includeTerritories = ( isDefault || typeof options.includeTerritories === "undefined" ) ? false : options.includeTerritories,
|
|
|
39 |
includeMilitary = ( isDefault || typeof options.includeMilitary === "undefined" ) ? false : options.includeMilitary,
|
|
|
40 |
regex;
|
|
|
41 |
|
|
|
42 |
if ( !includeTerritories && !includeMilitary ) {
|
|
|
43 |
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])$";
|
|
|
44 |
} else if ( includeTerritories && includeMilitary ) {
|
|
|
45 |
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])$";
|
|
|
46 |
} else if ( includeTerritories ) {
|
|
|
47 |
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])$";
|
|
|
48 |
} else {
|
|
|
49 |
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])$";
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
regex = caseSensitive ? new RegExp( regex ) : new RegExp( regex, "i" );
|
|
|
53 |
return this.optional( element ) || regex.test( value );
|
|
|
54 |
}, "Please specify a valid state" );
|