leepowelldev / mongoose-validator

Validators for mongoose models utilising validator.js
MIT License
378 stars 43 forks source link

Regex #11

Closed karan closed 10 years ago

karan commented 10 years ago

How do I check something in a regex?

Say I am checking a username for A-Z, a-z, 0-9, -, then what I tried didn't work:

var nameValidator = [validate({message: "Username should be between 4 and 30 characters."}, 
                        'len', 4, 30, 
                        "regex('/^[a-zA-Z\-]+$/')")];

And in my code, I have validators in 3 places (on top, then required, and then mongoose validation middleware):

I am a noob to this, and willing to learn. How do I make my code shorter, or at least easier to read, write and maintain?

leepowelldev commented 10 years ago

Try:

var nameValidator = [validate({message: "Username should be between 4 and 30 characters."}, 'regex', /^[a-zA-Z\-]+$/];
bmoeskau commented 10 years ago

Works for me! Might be worth adding a regex example to the README as the syntax (no enclosing quotes) is not always obvious

leepowelldev commented 10 years ago

Will do...