开发者

help with making this regExp in js

开发者 https://www.devze.com 2022-12-20 15:06 出处:网络
I need to compare a textarea to make sure: It\'s not ONLY SPACES and not ONLY RETURNS(new lines) and NOT ONLY a combination of these two.

I need to compare a textarea to make sure:

  1. It's not ONLY SPACES and not ONLY RETURNS(new lines) and NOT ONLY a combination of these two.
  2. However, if there are more than 3 characters OR numbers, then it is okay.
  3. Not more than say 2000 characters.

Imagine a textarea, I don't want the form to submit if the user have pressed enter (new line) 5 times, and I don't want it 开发者_运维问答to submit with only spaces. There must be atleast 3 characters or numbers.

Thanks


/^\s*(\w[^\w]*){3}.*$/

First, allow leading whitespace. Then match a letter/number followed by 0 or more non-letter/numbers, 3 times. Then also match any other characters found.

Will only match if there are at least 3 letter/number characters; they can have other characters interspersed between them.

As far as a max character limit goes, it'd be far simpler to just do this with a check on .length rather than trying to build it into the regex.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号