RegEx in jQuery Validator addMethod

I need some RegEx for my special jQuery validator validation methods. The first one, which I thought would be easy, is simply limiting the username to US letters, numbers, and special characters. I came up with ^[a-zA-Z0-9]+$, but it does not work. Perhaps this is due to how it appears in the function:

$.validator.addMethod(
    "legalName",
    function(value, element) {
        return (element.value != "^[a-zA-Z0-9]+$");
    },
    "Use a valid username."
);

The second is password verification, which is essentially the same restriction with the addition of the required number of certain characters, such as uppercase, numeric, etc. I understand, as soon as I get the RegEx username by password, it will not be too hard, just a question about installing in {1}, etc. In expression.

The last part of my question is how to add a second method ("legalPassword") to the original one .addMethodor do I need to create a whole different one $.validator.addMethod?

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

and table code:

<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>

New code (still not working):

$.validator.addMethod(
    "legalName",
    function(value, element) {
        /^[a-zA-Z0-9]+$/.test( value );
    },
    "Use a valid username."
);
0
source share
3 answers

return this.optional(element) || /^[a-zA-Z0-9]+$/.test( value );

+4
source

Try using the Regex.test function to check against your string.

/^[a-zA-Z0-9]+$/.test( element.value );

See here for more information: http://www.javascriptkit.com/javatutors/redev3.shtml

+2
source

Try connecting a Regex mask

View the following link

regexMask ()

0
source

All Articles