views:

25

answers:

1

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

+1  A: 
/^\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.

Amber