gpt4 book ai didi

javascript - 如何使用 jQuery 验证插件验证美国邮政编码

转载 作者:行者123 更新时间:2023-12-03 21:49:40 24 4
gpt4 key购买 nike

我正在使用jQuery Validation plugin用于验证我的表单。

我想按照美国邮政编码格式验证邮政编码:-

> 12345
> 12345-6789
> 12345 6789

根据我当前的代码,验证邮政编码最多应为 5,并且全部为数字。

请参阅下面的代码:

    <script src="~/Scripts/js/jquery.validate.js"></script>

<script>

$().ready(function () {

// validate signup form on keyup and submit
$("#locationSetup").validate({
rules: {
'Address.AddressStreet': "required",
'Address.City': "required",
'Address.State.Country.CountryIsoCode': "required",
'Address.State.SubnationalIsoCode': "required",
'Address.PostalCode': {
required: true,
minlength: 5,
maxlength: 5,
digits: true
}
},
messages: {
'Address.AddressStreet': "Please enter your Street Address!",
'Address.City': "Please select your City!",
'Address.State.Country.CountryIsoCode': "Please select your Country!",
'Address.State.SubnationalIsoCode': "Please select your State!",
'Address.PostalCode': {
required: "Please enter your Postal Code!",
minlength: "Your Postal Code must be 5 numbers!",
maxlength: "Your Postal Code must be 5 numbers!",
digits: "Your Postal Code must be 5 numbers!"
}
}
});

});
</script>

@using (Html.BeginForm(Model.Step.ToString(), "Provision", FormMethod.Post, new Dictionary<string, object> { { "id", "locationSetup" } }))

<table width="100%" id="locInfo">
<colgroup>
<col width="30%" />
<col />
</colgroup>
<tr>
<th><label>@AddressResource.COUNTRY_LABEL_TEXT:</label> <span class="redtext">(Required)</span></th>
<td>
@* @Html.HiddenFor(m => m.Address.State.Country.CountryIsoCode)*@
@Html.DropDownListFor(m => m.Address.State.Country.CountryIsoCode, ProvisioningHubProxy.GetCountriesList(),
htmlAttributes: new { @id = "Countries", @name = "Countries" })
</td>
</tr>
<tr>
<th> <label>@AddressResource.STATES_LABEL_TEXT:</label><span class="redtext">(Required)</span></th>
<td> @Html.DropDownListFor(m => m.Address.State.SubnationalIsoCode, ProvisioningHubProxy.GetStatesList(),
htmlAttributes: new { @id = "States", @name = "States" })</td>
</tr>
<tr>
<th><label>@AddressResource.CITY_LABEL_LABEL_TEXT:</label><span class="redtext">(Required)</span></th>
<td> @Html.TextBoxFor(m => m.Address.City, htmlAttributes: new { @name = "City", @id = "City" })</td>
</tr>
<tr>
<th> <label>@AddressResource.STREET_NAME_LABEL_TEXT:</label><span class="redtext">(Required)</span></th>
<td>@Html.TextBoxFor(m => m.Address.AddressStreet, htmlAttributes: new { @name = "StreetName", @id = "StreetName" })</td>
</tr>
<tr>
<th><label>@AddressResource.US_POSTAL_CODE_LABEL_TEXT:</label><span class="redtext">(Required)</span></th>
<td>@Html.TextBoxFor(m => m.Address.PostalCode, htmlAttributes: new { @name = "ZipCode", @id = "ZipCode", @required = "required" })</td>
</tr>
</table>

}

请提出建议。

最佳答案

添加自定义验证器:

jQuery.validator.addMethod("zipcode", function(value, element) {
return this.optional(element) || /^\d{5}(?:-\d{4})?$/.test(value);
}, "Please provide a valid zipcode.");

_如果您想在邮政编码之间接受空格1,请使用 /^\d{5}(?:[\s-]\d{4})?$/ 代替模式。

然后在您的选项中指定它:

rules: {
...
'Address.PostalCode': {
...
zipcode: true,
...
},
...
}

请注意扩展库 additional-methods.js还有一个 zipcodeUS 验证器(但除非您使用任何其他验证器,否则包含它可能没有意义)。

<小时/>

1 根据规范,格式正确的邮政编码由五 (5) 个数字或五 (5) 个数字后跟连字符 (-) 和四 (4) 个附加数字组成。空格技术上不应该被接受,但无论如何我都会提供模式。

关于javascript - 如何使用 jQuery 验证插件验证美国邮政编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19011993/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com