0

I need a few RegEx for my jQuery validator custom methods. The first which I thought was going to be easy was just limiting a username to only US letters, numbers, and no special characters. I came up with ^[a-zA-Z0-9]+$ but it doesn't seem to work. Maybe it is due to the way 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 validating the password which is essentially the same limitations with the addition of a required number of certain characters such as an uppercase, number, ect. I figure once I get the username RegEx down the password wont be too hard, just a matter of fitting in {1}'s and such within the expression.

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

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

and the 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."
);
2
  • You should never put ANY restrictions on passwords (except a minimum length, multiple types of characters, etc)! As you are (hopefully) storing them as a hash there is no good reason to do so anyway. Commented Mar 8, 2011 at 9:16
  • I am not storing anything, its simply for demonstration and won't even be validated for a database. Commented Mar 8, 2011 at 9:21

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

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

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

5 Comments

I tried replacing return (element.value != "^[a-zA-Z0-9]+$"); with /^[a-zA-Z0-9]+$/.test( element.value ); but now it is returning false on both legal and nonlegal usernames.
Does element.value contain the expected value? Try testing your regex using a hardcoded string to make sure it is OK. Regex looks OK to me. Your function takes (value, element) yet you are testing element.value, is that correct?
According to the tests the RegEx is working fine. As for the second part...I think yes? haha I am still pretty new to js/jquery. I added the rest of the code I have written myself for both the form validation and form html if it helps at all.
I would try testing against the value parameter instead of the element. Try: /^[a-zA-Z0-9]+$/.test( value )
I solved it by looking at the validator source and mimicking their code. return this.optional(element) || /^[a-zA-Z0-9]+$/.test( value ); If you want to change your original post I will mark it as the answer since you helped me get to this point and each change brought it closer. Thanks!
0

Try Regex Mask Plugin

Browse the following link

regexMask()

1 Comment

Thanks, but I would prefer to learn how to get this working without another plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.