The ASP Emporium
Free Active Server Applications and Examples by Bill Gearhart
Online since Friday January 7, 2000

 home > code > code library > ECommerce Procedures > isUSState Function

enter a phrase to search: (advanced search)


 h o m e 

 w h a t 's  n e w 

 a l l   c o d e 
  .net:
    • Fundamentals
    • C# Classes
  classic asp:
    • Code Library
    • ASP Apps
  general:
    • Tutorials
    • SQL

 d o w n l o a d s 

 u s e r   f o r u m s 

 l i n k s 

 s e a r c h 

 s u p p o r t 


isUSState Function   v1.0   [JScript]

< prev proc
IsState Function
next proc >
IsVBSReady Function

purpose:
This function will validate all 50 valid state abbreviations as well 
as DC. It's mainly useful for a scenario where you need someone's 
address, city, state and zip code and want to make sure that the state
abbreviation entered is a valid entry and not just random text.
syntax:
var string = isUSState(stateToValidateString);
example usage:
var string1 = "AL";
var string2 = isUSState(string1);
source code:
function isUSState(strIn) {
	/*
	validate 50 US States plus DC
	if you want to do puerto rico as well, 
	search the pattern below for
	|PA|
	and replace it with
	|P[AR]|
	*/
	var re = /^A[LKRZ]|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]$/gi
	return re.test(strIn);
	/*
	regular expression pattern broken down
	would look like this:
		^A[LKRZ]|      <-- AL, AK, AR, AZ
		C[AOT]|        <-- CA, CO, CT
		D[CE]|         <-- DC, DE
		FL|            <-- FL
		GA|            <-- GA
		HI|            <-- HI
		I[ADLN]|       <-- IA, ID, IL, IN
		K[SY]|         <-- KS, KY
		LA|            <-- LA
		M[ADEINOST]|   <-- MA, MD, ME, MI, MN, MO, MS, MT
		N[CDEHJMVY]|   <-- NC, ND, NE, NH, NJ, NM, NV, NY
		O[HKR]|        <-- OH, OK, OR
		PA|            <-- PA
		RI|            <-- RI
		S[CD]|         <-- SC, SD
		T[NX]|         <-- TN, TX
		UT|            <-- UT
		V[AT]|         <-- VA, VT
		W[AIVY]$       <-- WA, WI, WV, WY
	*/
}
< prev proc
IsState Function
next proc >
IsVBSReady Function