Avoid uppercase input with jQuery Validate
I'm making a classifieds ads website where users submit ads through a form (like Craigslist). Many users write the title or description of their ads in UPPERCASE to get more attention, but I dont want to affect the overall appearance of the website.
I would like to stop this, but still allow the use of acronyms of less than three uppercase letters in a row (for example allow 'Located in the USA' but not 'LOOK, GREAT OFFER')
I'm a complete newbie with Validate and RegEx (just one weekend) so I need your help experts.
I'm using this method:
$.validator.addMethod("noCaps", function(value, element) {
return this.optional(element) || /[A-Z]{4}/.test(value);
}, "You are not permitted to input more than three uppercase letters in a row!");开发者_Python百科
But I'm getting the complete opposite result. How could I negate this RegEx to achieve the desired rule?
If somebody has a better idea of how to do it, is also welcome!
Thanks!!!
See/Edit Example Code
You can just add a negation (!
) in front of it, like this:
$.validator.addMethod("noCaps", function(value, element) {
return this.optional(element) || !/[A-Z]{4}/.test(value);
}, "You are not permitted to input more than three uppercase letters in a row!");
You can give it a try here.
精彩评论