user1934643
user1934643

Reputation: 161

How to avoid continual repitition of characters in a text field?

I'd like to check a "textarea" field to make sure it doesn't contain characters repeated 3 or more times like listed below:

aaaaaaaaaaaaaaaaaa
ggggggggggggggg
ddddddddddddd

How can I do this using Javascript or Jquery?

Upvotes: 0

Views: 48

Answers (2)

Noctis
Noctis

Reputation: 11773

I agree with Pinch, but if you want to do it manually, you could always:

  1. Split the textarea into an array on white spaces (get an array of words)
  2. run in a loop on all words
  3. for each word, run in a loop on characters 0 to length-2, and check to see if the following 2 are not the same as the current.

Did I say I agree with pinch already? :)

Upvotes: 0

Pinch
Pinch

Reputation: 4207

You can accomplish this using regular expressions:

var hasDuplicate = (/^([a-zA-Z])\1+$/).test(str)

Upvotes: 2

Related Questions