简体   繁体   中英

Avoid uppercase input with jQuery Validation Engine

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.

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
  • (?!(?:[^AZ]*[AZ]){11}) - a negative lookahead that fails the match if, immediately to the right of the current position, there are
    • (?:[^AZ]*[AZ]){11} - 11 occurrences of
      • [^AZ]* - any 0+ chars other than uppercase ASCII letters
      • [AZ] - 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
  • (?!.*[AZ]{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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM