开发者

Regular Expression for username and password?

开发者 https://www.devze.com 2023-04-11 13:53 出处:网络
I am 开发者_JS百科trying to use a regular expression for name field in the asp.net application. Conditions:name should be minimum 6 characters ?

I am 开发者_JS百科trying to use a regular expression for name field in the asp.net application.

Conditions:name should be minimum 6 characters ?

I tried the following

"^(?=.*\d).{6}$"

I m completely new to the regex.Can any one suggest me what must be the regex for such condition ?


You could use this to match any alphanumeric character in length of 6 or more: ^[a-zA-Z0-9]{6,}$. You can tweak it to allow other characters or go the other route and just put in exclusions. The Regex Coach is a great environment for testing/playing with regular expressions (I wrote a blog post with some links to other tools too).


Look at Expression library and choose user name and/or password regex for you. You can also test your regex in online regex testers like RegexPlanet.

My regex suggestions are:

 ^[a-zA-Z][a-zA-Z0-9._\-]{5,}$

This regex accepts user names with minimum 6 characters, starting with a letter and containing only letters, numbers and ".","-","_" characters.

Next one:

 ^[a-zA-Z0-9._\\-]{6,}$

Similar to above, but accepts ".", "-", "_" and 0-9 to be first characters too.

If you want to validate only string length (minimum 6 characters), this simple regex below will be enough:

 ^.{6,}$


What about

^.{6,}$

What's all the stuff at the start of yours, and did you want to limit yourself to digits?


NRegex is a nice site for testing out regexes.

To just match 6 characters, ".{6}" is enough


In its simplest form, you can use the following:

.{6,}

This will match on 6 or more characters and fail on anything less. This will accept ANY character - unicode, ascii, whatever you are running through. If you have more requirements (i.e. only the latin alphabet, must contain a number, etc), the regex would obviously have to change.

0

精彩评论

暂无评论...
验证码 换一张
取 消