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.
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.
Share Improve this question edited Jun 10, 2015 at 12:01 Wiktor Stribiżew 628k41 gold badges498 silver badges614 bronze badges asked Jun 10, 2015 at 7:36 user2587454user2587454 9131 gold badge20 silver badges44 bronze badges 2- Please clarify: Are spaces disallowed thoughout or only in the first 6 characters? – acraig5075 Commented Jun 10, 2015 at 7:54
- no spaces at all. this is for password input. so 'abc123' is ok but 'abc 123' no. – user2587454 Commented Jun 10, 2015 at 9:30
2 Answers
Reset to default 7a 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.
^(?=.*\d)([\w]{6,})$
I think it should work fine