user2587454
user2587454

Reputation: 913

Regex for six characters with at least one digit

I am looking for a regex with at least 6 characters (no limit) including at least one digit. No spaces allowed.

I have this this regex:

^(?=.*\d).{4,8}$

However, I don't want to limit to 8 characters.

Upvotes: 3

Views: 4980

Answers (2)

Regexes
Regexes

Reputation: 105

^(?=.*\d)([\w]{6,})$

I think it should work fine

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627536

a regex with at least 6 characters (no limit) including at least one digit. no spaces allowed.

^(?=\D*\d)\S{6,}$

Or

^(?=\D*\d)[^ ]{6,}$

See demo

  • ^ Start of string
  • (?=\D*\d) - Must be 1 digit (the lookahead is based on the principle of contrast)
  • \S{6,} - 6 or more non-whitespaces
    OR
    [^ ]{6,} - 6 or more characters other than literal normal space

To enable the regex to match more than 6 characters, you only need to adjust the quantifier. See more about limiting quantifiers here.

Upvotes: 7

Related Questions