2

anyone ever worked with the jQuery Validate Plugin.

I have like the following function.

$("#registrationForm").validate({
        rules: {
            'user[login]': {
                required: true,
                minlength: 6,
                maxlength: 20
            },
            'user[email]': {
                required: true,
                email: true
            },

I want to check the user[login] input for [0-9a-zA-Z-_] !

Any idea how to do so? Only alphanum -_ should be allowed.

matt
  • 42,713
  • 103
  • 264
  • 397
  • 1
    This question might help you: http://stackoverflow.com/questions/280759/jquery-validate-how-to-add-a-rule-for-regular-expression-validation – J. Steen Jul 25 '11 at 19:50

1 Answers1

0

Wow, sorry: forgot that regexp is a custom method I've added in my own code!

Try this:

$.validator.addMethod("regexp", function(value, element, pattern) {
    return this.optional(element) || pattern.test(value);
}, "Field contains invalid characters.");

And then:

$("#my-form").validate({
    rules: {
        "my-field": {
            required: true,
            regexp: /^[0-9a-zA-Z_]*$/
        }
    }
});

Take a look here.

Original (wrong) answer:

I'm pretty sure you just need to add this:

regexp: /^[0-9a-zA-Z-_]*$/
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
  • thank you! however doesn't work: `Uncaught TypeError: Cannot call method 'call' of undefined` As soon as i type the 7th letter this error gets thrown. – matt Jul 25 '11 at 20:00
  • this is the documentation of the plugin. couldn't find anything: http://docs.jquery.com/Plugins/Validation – matt Jul 25 '11 at 20:02
  • thanks for the regexp! I needed to extend the plugin like J.Steen wrote in the comment. – matt Jul 25 '11 at 20:06