Jquery验证器addmethod无法正常工作

时间:2013-09-26 03:52:05

标签: javascript jquery

我的验证器添加方法有问题

(function($) {

    $.validator.passwordRating = function(password, username){
        if (!password || password.length < 8 ){
            alert(password);  (<--- problem)
            return rating(0, "too-short");
        }
    }
    $.validator.passwordRating.messages = {
        "similar-to-username": "Too similar to username",
        "too-short": "Too short",
        "very-weak": "Very weak",
        "weak": "Weak",
        "good": "Good",
        "strong": "Strong"
    }

$.validator.addMethod("password", function(value, element, usernameField) {
    // use untrimmed value
    var password = element.value,
    // get username for comparison, if specified
        username = $(typeof usernameField != "boolean" ? usernameField : []);

    var rating = $.validator.passwordRating(password, username.val());
    // update message for this field

    var meter = $(".password-meter", element.form);

    meter.find(".password-meter-bar").removeClass().addClass("password-meter-bar").addClass("password-meter-" + rating.messageKey);
    meter.find(".password-meter-message")
    .removeClass()
    .addClass("password-meter-message")
    .addClass("password-meter-message-" + rating.messageKey)
    .text($.validator.passwordRating.messages[rating.messageKey]);
    // display process bar instead of error message

    return rating.rate > 2;
}, "&nbsp;");
// manually add class rule, to make username param optional
$.validator.classRuleSettings.password = { password: true };

})(jQuery);

在上面的脚本中,当我将密码值更改回0时,它无法正常工作。从1 - &gt; 8没关系。

感谢大家的支持。

0 个答案:

没有答案