adi123 Posted June 21, 2011 Share Posted June 21, 2011 I have a zip code with this pattern if(preg_match("/zip/i",$field)) { if(!preg_match("/^[0-9]{5,5}(\-[0-9]{4,4})?$/", $value)) { $errors[] = "$value is not a valid zipcode."; } } the zip code is formed using the code below: function getStateCode() { $stateCode = array(1=> "AL" , "AK" , "AZ" , "AR" , "CA" , "CO" , "CT" , "DE" , "DC" , "FL" , "GA" , "HI" , "ID" , "IL" , "IN" , "IA" , "KS" , "KY" , "LA" , "ME" , "MD" , "MA" , "MI" , "MN" , "MS" , "MO" , "MT" , "NE" , "NV" , "NH" , "NJ" , "NM" , "NY" , "NC" , "ND" , "OH" , "OK" , "OR" , "PA" , "RI" , "SC" , "SD" , "TN" , "TX" , "UT" , "VT" , "VA" , "WA" , "WV" , "WI" , "WY" ); return $stateCode; } function getStateName() { $stateName = array(1=> "Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District of Columbia", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"); return $stateName; } The code above doesn't form a valid zip code CODE tags are neat. Quote Link to comment Share on other sites More sharing options...
fugix Posted June 21, 2011 Share Posted June 21, 2011 How are you fiing a zip code from those two arrays. And your regexp doesn't need a min, max if they are the same, see revised. if(!preg_match("/^[0-9]{5}(\-[0-9]{4})?$/", Quote Link to comment Share on other sites More sharing options...
requinix Posted June 21, 2011 Share Posted June 21, 2011 The code above doesn't form a valid zip code It doesn't form any kind of zip code at all. The only things you posted (1) do a couple quick zip code-related tests on a couple variables and (2) return arrays of state names and abbreviations. There are no zip codes anywhere. There is nothing that creates zip codes. Perhaps you forgot to post something? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.