[英]Regex for validating postcode and phone number
I am creating a form, with warning elements.我正在创建一个带有警告元素的表单。 All works great but need to define my patterns better.一切都很好,但需要更好地定义我的模式。
$("#postcode").inputNotes(
{
warning: {
pattern: /^[0-9]+$/,
type: 'note',
text: 'Only numbers, please ...',
inversedBehavior: true
}
}
);
$("#phone").inputNotes(
{
warning: {
pattern: /^[0-9]+$/,
type: 'note',
text: 'Only numbers, no spaces please ...',
inversedBehavior: true
}
}
);
Ok for the Postcode warning, I want the pattern to expect 4 numbers好的邮政编码警告,我希望模式期望 4 个数字
For the Phone warning, I want the pattern to expect 10 numbers.对于电话警告,我希望该模式期望 10 个数字。
Any help appreciated.任何帮助表示赞赏。
Here are the rules for Australian postcodes:以下是澳大利亚邮政编码的规则:
ACT: 0200-0299 and 2600-2639.行动:0200-0299 和 2600-2639。
NSW: 1000-1999, 2000-2599 and 2640-2914.新南威尔士州:1000-1999、2000-2599 和 2640-2914。
NT: 0900-0999 and 0800-0899. NT:0900-0999 和 0800-0899。
QLD: 9000-9999 and 4000-4999.昆士兰州:9000-9999 和 4000-4999。
SA: 5000-5999.南非:5000-5999。
TAS: 7800-7999 and 7000-7499.塔斯马尼亚州:7800-7999 和 7000-7499。
VIC: 8000-8999 and 3000-3999.维多利亚州:8000-8999 和 3000-3999。
WA: 6800-6999 and 6000-6799华盛顿州:6800-6999 和 6000-6799
Regular Expression: ^(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})$正则表达式:^(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})| (290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2} )$
Pass: 0200|||7312|||2415通行证:0200|||7312|||2415
Fail: 0300|||7612|||2915失败:0300|||7612|||2915
Australia Post has greatly simplified the rules in 2019. You now only need to account for the ranges:澳大利亚邮政在 2019 年大大简化了规则。您现在只需要考虑范围:
0200 – 0299, 0800-0999 and 1000 – 9999 0200 – 0299、0800-0999 和 1000 – 9999
So the Regular Expression for Validating Australian Postcodes is just:所以验证澳大利亚邮政编码的正则表达式就是:
^(0[289][0-9]{2})|([1-9][0-9]{3})$
Update:更新:
For 6 digit or 10 digit numbers: pattern: /^[0-9]{10}|[0-9]{6}$/,
对于 6 位或 10 位数字: pattern: /^[0-9]{10}|[0-9]{6}$/,
For four digit Postcode: pattern: /^[0-9]{4}$/,
对于四位邮政编码: pattern: /^[0-9]{4}$/,
For 10 digit Phone number: (if phone number can't start with 0) pattern: /^[1-9][0-9]{9}$/,
对于 10 位电话号码:(如果电话号码不能以 0 开头) pattern: /^[1-9][0-9]{9}$/,
(if phone number can start with 0) pattern: /^[0-9]{10}$/,
(如果电话号码可以以 0 开头) pattern: /^[0-9]{10}$/,
For a 4 digit postcode, you can use /^\d{4}$/
.对于 4 位邮政编码,您可以使用/^\d{4}$/
。
For a 10 digit postcode, you can use /^\d{10}$/
.对于 10 位邮政编码,您可以使用/^\d{10}$/
。
Keep in mind some people will type spaces, parenthesis etc into the phone number.请记住,有些人会在电话号码中输入空格、括号等。 You should strip all non digits first before validating with /[^\d]+/g
.在使用/[^\d]+/g
进行验证之前,您应该先去除所有非数字。
Instead of using the +
modifier, try using {num}
, where num
is the number of instances of the previous atom you want.不要使用+
修饰符,而是尝试使用{num}
,其中num
是您想要的前一个原子的实例数。
All these answers are assuming that you limit the text field to 4 characters eg.所有这些答案都假设您将文本字段限制为 4 个字符,例如。 (12345) is valid, to limit the length use the following (12345)有效,限制长度使用以下
^(0[289][0-9]{2})$|^([1-9][0-9]{3})$
just to save people time here is an example of how to use in jquery只是为了节省人们的时间,这里是如何在 jquery 中使用的示例
$("#address-postal-code").once().on('blur', function (e) {
var myRegEx = new RegExp('^(0[289][0-9]{2})|([1-9][0-9]{3})$');
var ele = $(this);
if (!myRegEx.test(ele.val())) {
console.log('no good');
}
else {
console.log('good');
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.