【问题标题】:Don’t allow Gmail, Yahoo, etc email addresses with jQuery validate without regular expression [duplicate]不允许使用 jQuery 验证 Gmail、Yahoo 等电子邮件地址而不使用正则表达式 [重复]
【发布时间】:2018-02-10 13:35:07
【问题描述】:

不允许用户使用免费电子邮件地址注册。必须仅使用 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"
            }
        }
    });

【问题讨论】:

  • 你的问题到底是什么?到目前为止你做了什么来解决它?
  • docs 表明您可以添加depends 选项。然后,您可以在电子邮件字段中定义一系列不允许的关键字(gmail、yahoo 等)

标签: jquery jquery-validate


【解决方案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
        }
    },

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-24
    • 2021-05-02
    • 2023-04-05
    相关资源
    最近更新 更多