1

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.

1 Answer 1

3

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.
Sign up to request clarification or add additional context in comments.

Comments

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.