Reputation: 5974
I need a regular expression which fulfills below requirements.
Tried this, but its not working as expected.
^[a-z\s]{0,50}[.\-']*[a-z\s]{0,50}[.\-']*$
Please let me know, if some one gets this right.
Upvotes: 0
Views: 188
Reputation:
Well, you could either write some monstrous regexp, which would be impossible to read or maintain, or just write code which says what the rules are:
function validate(str) {
var not_too_long = str.length <= 50,
has_no_dots = !/\./.test(str),
not_too_many_specials = (str.match(/[^\w\s]/g) || []).length <= 3;
return not_too_long && has_no_dots && not_too_many_specials;
}
with appropriate adjustments for your definition of "special characters".
Upvotes: 4