jQuery 验证电子邮件验证问题

jQuery Validation Email Validation Issues

我正在尝试解决并修复我的电子邮件验证无法正常工作的原因。假定检测无效电子邮件:它有太多...(点),除了欧洲地址(test@tom.uk.tk)和 test@aol.com。但就目前而言,除了一个点之外,如果你没有完成输入,只要它有 @ 只要你不添加 (.) 点。有人能帮我解决我哪里出错了吗?

<script>
     $("#form").validate({
    rules: {
        firstname_1: {
            required: true
        },
        email_1: {
            required: true,
            email: true
        },
        // Same for other fields
    },
    messages: {
        firstname_1: "This field is required.",
        email_1>: "Please enter valid email address.",
        // Repeat for other fields
    }
});
function isValidEmail(email_1)
{
    return /^[a-z0-9]+([-._][a-z0-9]+)*@([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,4}$/.test(email_1)
        && /^(?=.{1,64}@.{4,64}$)(?=.{6,100}$).*/.test(email_1);
}
</script>

假设您已经拥有正确的验证逻辑 and/or 正则表达式,那么您需要使用 the .addMethod() method.

创建自己的规则
$("#form").validate({
    rules: {
        firstname_1: {
            required: true
        },
        email_1: {
            required: true,
            myEmail: true  // <- declare your custom rule
        },
        // Same for other fields
    },
    messages: {
        firstname_1: "This field is required.",
        email_1: "Please enter valid email address.",
        // Repeat for other fields
    }
});

// create your custom rule
jQuery.validator.addMethod("myEmail", function(value, element) {
    return this.optional(element) || /* your regex boolean logic goes here */;
}, 'Please enter valid email address.');

注意:您在 messages 中的字段名称上也有 syntax/spelling 错误。应该是 email_1,而不是 email_1>.

See this answer 的正确使用方法 .addMethod().