Shrikant Gupta
Shrikant Gupta

Reputation: 131

Checking length of text-box content in javascript with a regex

I use the following regex to validate a text box's content:

^[A-Za-z.]*[A-Za-z][-A-Za-z0-9,/()&:. ]*$

I would like to validate the length of my text-box in this regexp.

Upvotes: 0

Views: 552

Answers (1)

instanceof me
instanceof me

Reputation: 39148

To check a string's length and format with a regex, you can use numbered repeaters:

^[a-z]{1,128}$

However, if you have a succession of unkown numbers of character classes, you can use zero-length positive lookeahead at the beginning of the regex:

^(?=.{1,128}$)[a-z]*[a-z0-9]*$

So for your regex:

^(?=.{1,128}$)[A-Za-z.]*[A-Za-z][-A-Za-z0-9,/()&:. ]*$

If you can though, I'd still suggest checking myString.length instead.

Upvotes: 1

Related Questions