應處理輸入的數據以檢查郵政編碼是否包含在服務區域中(這包括郵政編碼63121至69999)。模式匹配檢查範圍內的郵政編碼
這就是我所做的我知道我在做模式匹配錯誤。任何幫助將不勝感激!
function validate() {
var zipCode = document.getElementById("zip");
var ok = zipCode.value.search(/^(6|9)\d{3,9}\d{1,9}\d{2,9}\d{1,9}$/);
if (ok != 0) {
alert("Sorry no service");
zipCode.focus();
zipCode.select();
return false;
} else
return true;
}
HTML部分
<form name = "myForm" action = "" >
<table align = "center" bgcolor = "FF6600" cellspacing = "3" cellpadding = "4" border = "1" style="font-family:Comic Sans;vertical-align:center;">
<caption style="background-color:#FFD700; text-align:left"> <font size="5" color="black">Check Coverage: </font></caption>
<tr>
<td class="info">First Name</td>
<td><input type = "text" name = "first" class="textright">*</td>
</tr>
<tr>
<td class="info">Phone Number</td>
<td><input type = "text" name = "phone" size="10" maxlength = "12" class="textright" >* (ddd-ddd-dddd)</td>
</tr>
<tr>
<td class="info">Zip Code</td>
<td><input type = "text" id="zip" name = "zip1" size ="5" maxlength = "5" class="textright">* </td>
</tr>
<tr>
<td class="size">* - Required Field</td>
<td><input type = "button" class="but2" value ="Reset"><input type = "submit" class= "but2" onclick="validate()" value ="Check Availability" ></td>
</tr>
</table>
</form>
這是一個重複 - http://stackoverflow.com/questions/578406/what-is-the-ultimate-postal-code-and -zip-regex? – Leptonator
@Leptonator不是一個愚蠢的事情,因爲這是在詢問一個非常具體的郵編範圍。 – 2013-10-30 04:36:39
我已更新答案以符合更新後的問題。但@MikeEdwards建議:未來,您可能要麼提出兩個問題,要麼更好地確定最初的問題。 – 2013-10-30 05:52:14