电子邮件规则jQuery验证

时间:2013-12-24 09:42:25

标签: javascript jquery jquery-validate

最好,

我想使用jQuery验证(插件),现在我得到的电子邮件规则没有正确放置。谁能帮我?

伯特

jQuery.validator.setDefaults({
    debug: true,
    success: "valid"
});
$("#contactFormm").validate({
    rules: {
        field: {
            required: true,
            email: true
        }
    },
    invalidHandler: function (event, validator) {
        // 'this' refers to the form
        var errors = validator.numberOfInvalids();
        if (errors) {
            var message = errors == 1 ? 'Er is 1 veld niet (correct) ingevuld' : 'Er zijn ' + errors + ' velden niet (correct) ingevuld.';
            $("div.error span").html(message);
            $("div.error").show();
        } else {
            $("div.error").hide();
        }
    }
});

1 个答案:

答案 0 :(得分:2)

更改field

field: {
    required: true,
    email: true
}

email

email: {
    required: true,
    email: true
}

field表示您的字段名称。所以假设你的名字是<input name="email" />

完整代码:

$("#contactFormm").validate({
    rules: {
        email: {
            required: true,
            email: true
        },
    },
    invalidHandler: function (event, validator) {
        // 'this' refers to the form
        var errors = validator.numberOfInvalids();
        if (errors) {
            var message = errors == 1 ? 'Er is 1 veld niet (correct) ingevuld' : 'Er zijn ' + errors + ' velden niet (correct) ingevuld.';
            $("div.error span").html(message);
            $("div.error").show();
        } else {
            $("div.error").hide();
        }
    }
});