jQuery验证插件:仅接受美国,加拿大和墨西哥的邮政编码? [英] jQuery validation plugin: accept only US, Canada and Mexic zip code?

查看:109
本文介绍了jQuery验证插件:仅接受美国,加拿大和墨西哥的邮政编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jQuery验证插件来验证仅接受美国,加拿大或墨西哥邮政编码格式的邮政编码字段,但是似乎没有为此定义的规则.我已经搜索过Google,但没有发现任何有用的信息.

I'd like to use jQuery validation plugin to validate a zip code field that accept only US, Canadian or Mexican zip code format, but there doesn't seem to be a defined rule for it. I've searched Google but I've found nothing useful.

我只接受以下邮政编码格式:

I have to accept only these zip code formats:

美国:99999-9999或99999 加拿大:A9A 9A9 MX:99999

US: 99999-9999 or 99999 Canada: A9A 9A9 MX: 99999

感谢您的帮助.

推荐答案

查看

Look inside the additional-methods.js file for the various zip code rules. You can copy and tweak them as you see fit.

jQuery.validator.addMethod("ziprange", function(value, element) {
    return this.optional(element) || /^90[2-5]\d\{2\}-\d{4}$/.test(value);
}, "Your ZIP-code must be in the range 902xx-xxxx to 905-xx-xxxx");

jQuery.validator.addMethod("zipcodeUS", function(value, element) {
    return this.optional(element) || /\d{5}-\d{4}$|^\d{5}$/.test(value);
}, "The specified US ZIP Code is invalid");

这篇关于jQuery验证插件:仅接受美国,加拿大和墨西哥的邮政编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆