Simple jQuery Validator addMethod not working

Updated question below

I am trying to verify a super simple form. In the end, the username will be compared with the RegExp statement, and the same will be for the password. However, now I'm just trying to learn the Validator addMethod format.

I currently have this script:

JQuery.validator.addMethod(
    "legalName",
    function(value, element) {
        if (element.value == "bob")
        {
            return false;
        }
        else return true;
    },
    "Use a valid username."
);

$(document).ready(function() {
    $("#form1").validate({
        rules: {
            username: {
                legalName: true
            }
        },
    });
});

What if I'm not mistaken, I should return false and answer "Use a valid username". if I had to put "bob" in the form.

However, he just sends it.

I am contacting the jQuery BEFORE Validator in the header as indicated.

My simple form is as follows:

<form id="form1" method="post" action="">
  <div class="form-row"><span class="label">Username *</span><input type="text" name="username" /></div>
  <div class="form-row"><input class="submit" type="submit" value="Submit"></div>
</form>

, addMethod, true if false else, false? ( , , :))

.


, JQuery โ†’ JQuery.

โ†’ RegEx jQuery Validator addMethod

( /). , - , js. , < 48 , , . , , - : ^[a-zA-Z0-9]*${1,48}, , JS RegExp ( Ruby RegExp, ?... rubular.com), , / .

, $.validator.addMethod legalPassword, .

Edit

^[a-zA-Z0-9]+$ maxlength: 48 . regexp , , .

+3
3

JQuery, JQuery ( j).

JavaScript ...

>>> typeof jQuery
"function"
>>> typeof JQuery
"undefined"

$ JQuery ( ), .

, ...

jQuery.validator.addMethod(
    "legalName",
    function(value, element) {
        return (element.value != "bob");
    },
    "Use a valid username."
);

toLowerCase() value.

+4

, JQuery.validator.addMethod $.validator.addMethod.

. , , , .

+1

jQuery.validator.addMethod("legalname", function(value, element) {
    var i = /^bob$/i;
    return (!(i.test(value)));
}, "Invalid user name");

. DEMO

0

All Articles