2013-10-01 22 views
0

我有一個依賴於包含50個美國中的38個的數組的警報,它工作正常(感謝您的幫助,美妙的人),但現在我擔心用戶可能會輸入格式的變化我目前使用的是兩個大寫字母(即WA,OR,GA等)。如果他們要輸入小寫字母(即wa或ga)或者可能是大寫和小寫字母(即Wa,Or,Ga)或符號(即WA - 華盛頓,OR - 俄勒岡州,喬治亞州)的組合。想知道是否有更準確的方法來做到這一點,以捕捉所有變化。確定Javascript數組的變化

我指的是最後一個,在底部指向#address_province。

請注意我非常喜歡使用Javascript和jQuery,因爲您可以提供更多的細節。

非常感謝您提前。

<script> 
// Hides shipping info fieldset if ship to billing is checked 
$(function() { 
    $("#ship_to_billing").change(function() { 
     if ($(this).is(":checked")) $("#shipping_info").hide(); 
     else $("#shipping_info").show(); 
    }); 
}); 

// Validates address fields are filled out unless ship to billing is checked... 
function validateShipping() { 
    if (!$("#ship_to_billing").is(":checked")) { 
     var inputs = $("#shipping_info input"); 
     var ready = true; 
     inputs.each(function() { 
      if ($(this).val() == '') { 
       ready = false; 
       return false; 
      } 
     }); 
     if (!ready) { 
      alert("Please tell us where to send this. Either choose ship to Billing Address or fill out both the Recipient Name as well as Shipping Address fields. Thanks!"); 
      return false; 
     } 
    } 
     // Makes sure age verification is checked 
     if (!$('#age_verification').is(':checked')) { 
      alert("Please verify you are 21 years of age or older."); 
      return false;  
     } 
     // Confirms province is allowed for wine shipping 
      var states = ["AK", "AZ", "CA", "CO", "CT", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "LA", "ME", "MD", "MI", "MN", "MO", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OR", "SC", "TN", "TX", "VT", "VA", "WA", "WV", "WI", "WY"]; 
       if ($.inArray($("#address_province").val(), states) <0) { 
       alert("Shipping gifts containing alcohol to this state is prohibited by law. Please choose another item."); 
       return false;  
     } 
return true; 
} 

</script> 
+0

取輸入值的前兩個字符,轉換爲大寫,然後比較?或者使用僅包含實際有效的狀態的'