Regex, detect no spaces in the string

This is my current regex check:

const validPassword = (password) => password.match(/^(?=.*\d)(?=.\S)(?=.*[a-zA-Z]).{6,}$/);

I have a check for at least 1 letter and 1 number and at least 6 characters long. However I also want to make sure that there are no spaces anywhere in the string.

So far I'm able to enter in 6 character strings with spaces included :(<img src=https://www.e-learn.cn/content/wangluowenzhang/"https://i.stack.imgur.com/tpPBh.png" alt="enter image description here">

Found this answer here, but for some reason in my code it's passing.

What is the regular expression for matching that contains no white space in between text?

Answer1:

It seems you need

/^(?=.*\d)(?=.*[a-zA-Z])\S{6,}$/

<strong>Details</strong>

    <li>^ - start of string</li> <li>(?=.*\d) - 1 digit (at least)</li> <li>(?=.*[a-zA-Z]) - at least 1 letter</li> <li>\S{6,} - 6 or more non-whitespace chars</li> <li>$ - end of string anchor</li> </ul>

    With a principle of contrast in mind, you may revamp the pattern into

    /^(?=\D*\d)(?=[^a-zA-Z]*[a-zA-Z])\S{6,}$/

    人吐槽 人点赞

Recommend

Comment

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:Regex, detect no spaces in the string