不使用正则表达式,不允许使用jQuery验证的Gmail,Yahoo等电子邮件地址

时间:2017-09-01 13:12:48

标签: jquery jquery-validate

不允许用户使用免费电子邮件地址注册。必须只使用jQuery Validate Plugin

$("form").validate({
        rules: {
            username: {
                required: true,
                minlength: 2,
                nofreeemail: true
            }
        },
        messages: {
            username: {
                required: "Please enter a username",
                minlength: "Your email must consist of at least 2 characters",
                nofreeemail: "Please use your business email"
            }
        }
    });

1 个答案:

答案 0 :(得分:1)

添加新方法:

$.validator.addMethod('noemail', function (value) {
    return /^([\w-.]+@(?!gmail\.com)(?!yahoo\.com)(?!hotmail\.com)(?!mail\.ru)(?!yandex\.ru)(?!mail\.com)([\w-]+.)+[\w-]{2,4})?$/.test(value);
}, 'Free email addresses are not allowed.');

rules: {
        username: {
            required: true,
            minlength: 2,
            noemail: true
        }
    },