Reputation: 473
Some users a writing their messages in uppercase only, and I want to avoid that with JQuery Validation Engine.
I have tried many many regex without any success. Here is the idea for a custom rule to avoid more than 10 uppercase characters:
uppercase: {
regex: /^(![A-Z]{10})+$/,
alertText: "* uppercase test alert"
},
I can't figure out what's wrong.
Upvotes: 1
Views: 237
Reputation: 626729
If you want to only allow strings with 10 and fewer uppercase letters, you may use
/^(?!(?:[^A-Z]*[A-Z]){11})/
See the regex demo
The pattern matches any string that does not contain 11 or more ASCII uppercase letters (so, it may contain 0 to 10 ASCII uppercase letters).
Details
^
- start of string(?!(?:[^A-Z]*[A-Z]){11})
- a negative lookahead that fails the match if, immediately to the right of the current position, there are
(?:[^A-Z]*[A-Z]){11}
- 11 occurrences of
[^A-Z]*
- any 0+ chars other than uppercase ASCII letters[A-Z]
- an uppercase ASCII letter.If you want to match a string that has no 10 uppercase ASCII letters on end:
/^(?!.*[A-Z]{11})/
See the regex demo.
Details
^
- start of the string(?!.*[A-Z]{11})
- a negative lookahead that fails the math if there are 11 uppercase ASCII letters after any 0+ chars other than line break chars immediately the right of the current location.Upvotes: 3