0

应处理输入的数据以检查邮政编码是否包含在服务区域中(这包括从 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>
4

2 回答 2

2

无需使用正则表达式。由于您正在查看的所有邮政编码都是 5 位数长(没有前导零),请将它们转换为整数并检查邮政编码是否在所需范围内。

function validate() {
    var zipCodeField = document.getElementById("zip");
    var zipCode = parseInt(zipCodeField.value);
    if (!(63121 <= zipCode && zipCode <= 69999)) {
        alert("Sorry no service");
        zipCodeField.focus();
        zipCodeField.select();
        return false;
    } else {
        return true;
    }
}

由于有关它的更新不起作用,我把这个小提琴放在一起。您唯一需要做的是函数return的值,表单的值而不是按钮的值。validate()onsubmitonclick

于 2013-10-30T04:36:12.310 回答
-1

我看到你的要求是线性的..

var x=parseInt(zipcode)
if(zipcode<63121 and zipcode>69999){
alert("Sorry no service");
        zipCode.focus();
        zipCode.select();
        return false;
} 
else 
{
return true;
}
于 2013-10-30T04:39:51.517 回答