-1

The method0~method4 work not well. I add this into validator of jQuery. The wired result shows. How can I fix it.


var conditions = [
    {
        "regexp": ".{8,}",
        "message": ""
    },
    {
        "regexp": "[A-Z]",
        "message": ""
    },
    {
        "regexp": "[0-9]",
        "message": ""
    },
    {
        "regexp": "(.)\\1{3}",
        "message": ""
    },
    {
        "regexp": "[a-z]",
        "message": ""
    }
];

    for (var i in conditions) {
        var methodName = "method" + i;
        pwdRules[methodName] = true;
        pwdMsgs[methodName] = conditions[i].message;

        $.validator.addMethod(
            methodName,
            function(value, element) {
                return value.match(conditions[i].regexp) != null;
            },
            conditions[i].message
        );
    }


Sparky
  • 98,165
  • 25
  • 199
  • 285
Glenn89
  • 1
  • 1
  • Not quite sure what you are asking here? Could just be me, but what are you validating here? Where is 'It looks like your post is mostly code; please add some more details.' coming from? Are we missing some of your code? – tmkiernan Sep 20 '19 at 10:32
  • @tmkiernan, the OP is bypassing the warning system. Instead of writing a better description with more details as prompted, he's filling it up with the SO message, *"It looks like your post is mostly code; please add some more details."* – Sparky Sep 20 '19 at 18:36
  • As it stands your question needs more of the relevant code and a better description. Please fix your question. No idea why you think you need to dynamically write a custom method for each individual condition. Instead you can pass the condition into the method as a parameter. – Sparky Sep 20 '19 at 18:39

1 Answers1

0

There is absolutely no reason to create multiple versions of the same custom method. Create one custom method and pass the different parameters into it as params.

$.validator.addMethod(
    myMethodName,
    function(value, element, params) {
        // one parameter is just passed in as a number or string and accessed directly as params
        // multiple parameters are passed as an array ["first", "second", "etc"]
        // first parameter is params[0]
        // second parameter is params[1], etc.
    },
    "custom message"
);

The custom message can be a plain string...

"this is my custom message"

or a string that dynamically inserts the parameters into the message using the placeholders {0}, {1}, etc...

"your entered value must be at least {0} and less than {1}"

or a function that constructs any message you wish...

function() {
    var message;
    if (params == "something") {
        message = "my custom message for something";
    } else {
        message = "my default";
    }
    return message;
}

However, if you simply want to use regex, then there is already a method/rule called pattern built into the additional-methods.js file, which will take regex as the parameter.

See: jQuery validate: How to add a rule for regular expression validation?

Sparky
  • 98,165
  • 25
  • 199
  • 285