jQuery custom validation: phone number starting with 6

元气小坏坏 提交于 2019-11-29 05:11:00
dheerosaur

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");

and use it like this:

rules: {
    phone: {
        required: true,
        minlength: 9,
        phoneEnding6: true
    },..

You need to edit the regex inside phone_number.match to suit your requirements.

jack

I think you will need to add a custom validator.

$.validator.addMethod("phoneNumber", function(uid, element) {
    return (this.optional(element) || uid.match(phone number regex));
}

You can also try simple Javascript function

"Hello World!".startsWith("He"); //true

Note: This comment/code is for those who are not using jquery validate.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!