jQuery自定义验证:电话号码以6开头 [英] jQuery custom validation: phone number starting with 6
本文介绍了jQuery自定义验证:电话号码以6开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表格,您必须在其中输入您的电话号码和其他字段.
I have a form in which you have to input your phone number and other fields.
我正在使用jQuery Validate验证表单.
I'm validating the form with jQuery Validate.
要验证电话号码字段,我要做:
To validate the phone number field I do:
rules: {
phone: {
required: true,
minlength: 9,
number:true
},..
但是我还需要检查手机是否以6开头.
But I need to check also that phone starts with 6.
我该怎么做?
推荐答案
您可以添加自定义验证器
You can add a custom validator
jQuery.validator.addMethod("phoneStartingWith6", function(phone_number, element) {
phone_number = phone_number.replace(/\s+/g, "");
return this.optional(element) || phone_number.match(/^6\d{8,}$/);
}, "Phone number should start with 6");
并像这样使用它:
rules: {
phone: {
required: true,
minlength: 9,
phoneEnding6: true
},..
您需要在phone_number.match
中编辑正则表达式以适应您的要求.
You need to edit the regex inside phone_number.match
to suit your requirements.
这篇关于jQuery自定义验证:电话号码以6开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文