开发者

How to Toggle Multiple arguments of the function

Please check my J Query program. I have much parameter inputs in a IF Statements so Is there any way that it will toggle each argument? My Current Program is working fine but I need more combinations . For example there are four combination so 4*4 = 16 Combination for each IF so is there any solution to minimize parsing all arguments?

    e.g lower && upper && digit && special
    lower && special &&  upper && digit 
    upper && special &&  digit && upper 
    .
    .
    .
    special && digit && upper && lower

(function($) {

var LOWER = /[a-z]/,
    UPPER = /[A-Z]/,
    DIGIT = /[0-9]/,
    DIGITS = /[0-9].*[0-9]/,
    SPECIAL = /[^a-zA-Z0-9]/,
    SAME = /^(.)\1+$/;

function rating(rate, message) {
    return {
        rate: rate,
        messageKey: message
    };
}

function uncapitalize(str) {
    return str.substring(0, 1).toLowerCase() + str.substring(1);
}

$.validator.passwordRating = function(password, username) {

    if (!password || password.length < 5)
        return rating(0, "too-short");

    if (username && password.toLowerCase().match(username.toLowerCase()))
        return rating(0, "similar-to-username");

    if (SAME.test(password))
        return rating(1, "very-weak");

        var lower = LOWER.test(password),
        upper = UPPER.test(uncapitalize(password)),
        digit = DIGIT.test(password),
        digits = DIGITS.test(password),
        special = SPECIAL.test(password);



    if (lower && upper && digit && special && password.length > 19 || lower && upper && digits && special && password.length > 19 || special && upper && lower && digits && password.length > 19 || lower && special && upper && digits && password.length > 19 || lower && upper && special && digits && password.length > 19 || upper && lower && special && digits && password.length > 19)
        return rating(5, "very-strong");

    if (lower && upper && digit && special && password.length > 8 || lower && upper && digits && special && password.length > 8 || special && upper && lower && digits && password.length > 8 || lower && special && upper && digits && password.length > 8 || lower && upper && special && digits && password.length > 8 || upper && lower && special && digits && password.length > 8)
        return rating(4, "strong");

    if (lower && upper && digit && password.length > 5 || lower && upper && digits && password.length > 5|| upper && lower && digits && password.length > 5 || upper && lower && digit && password.length > 5 || digit && upper && lower && password.length > 5 ||  digits && upper && lower && password.length > 5 || upper && digit && lower && password.length > 5 || upper && digits && lower && password.length > 5)
        return rating(3, "good");
        return rating(2, "weak");

}

$.validator.passwordRating.messages = {
    "similar-to-username": "Too similar to username",
    "too-short": "Too short",
    "very-weak": "Very weak",
    "weak": "Weak",
    "good": "Good",
    "strong": "Strong",
    "very-strong": "Very Strong"
}

$.validator.addMethod("password", function(value, element, usernameField) {
    // use untrimmed value
    var password = element.value,
    // get username for comparison, if specified
    开发者_如何学Go    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);


how about using a scoring system for the acceptable scores?

  var score = 5;

  if (!(lower && upper)) {
    score--;
  }
  if (passwordlength <= 8) {
    score--;
  }
  if (!digit) {
    score--;
  }

and then for the blockers, like not having a password match the username, just have an error message.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜