简体   繁体   English

正则表达式字母数字字符-允许连字符和下划线

[英]regular expression alphanumeric characters - allowing hyphens and underscores

I need a Regular Expression for Javascript that checks if a password is correct only when 我需要一个Java正则表达式,仅在以下情况下才检查密码是否正确:

  • Has at least 1 number and 1 letter 至少有1个数字和1个字母
  • Is still valid when it has an underscore and/or a hyphen 下划线和/或连字符时仍然有效
  • Must be 4-20 characters long. 必须是4-20个字符长。

Examples: 例子:

  1. test123 -> Valid test123->有效
  2. test1 -> valid test1->有效
  3. 1234 ->invalid 1234->无效
  4. test -> invalid 测试->无效
  5. test1_ -> valid test1_->有效
  6. test-2 -> valid 测试2->有效

I tried using 我尝试使用

var Reg = /^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z-_]{4,12}$/;

It works in PHP, but not in Javascript, any suggestions? 它可以在PHP中运行,但不能在Javascript中运行,有什么建议吗?

Put the hyphen at the end of the character class: 将连字符放在字符类的末尾:

var Reg = /^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z_-]{4,20}$/;

You can also shorten that character class: 您也可以缩短该字符类:

var Reg = /^(?=.*\d)(?=.*[A-Za-z])[\w-]{4,20}$/;

Also, you said 20 characters, not 12. Right? 另外,您说的是20个字符,而不是12个。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 正则表达式(连字符,下划线和句点) - Regular Expression (Hyphens, Underscores, and Periods) Javascript正则表达式:匹配所有字母数字字符,除所有空白外,允许使用破折号和下划线 - Javascript regular expression: Match all alphanumeric characters, allow dashes spaces and underscores except all whitespace JavaScript正则表达式(带_-的字母数字字符) - JavaScript regular expression ( alphanumeric characters with _-) 字母数字字符和-/仅的正则表达式 - Regular Expression for Alphanumeric characters and - / only JQuery正则表达式接受字母数字字符和&#39;, - - JQuery Regular expression to accept alphanumeric characters and ' , - JS 正则表达式 允许字母数字字符和。 , " ( ) - _ #: ; * @ &amp; 不剩余 +, =, $, ,, &lt;, &gt;, `, ~, {, }, |,\?,, ~ - JS regular expression Allow alphanumeric characters and . , " ( ) - _ # : ; * @ & not remaining +, =, $, !, <, >, `, ~, {, }, |,\,?, ~ javascript-正则表达式字母数字和特殊字符 - javascript- regular expression alphanumeric and special characters [字母数字] [字母数字.__ @] 31个字符的正则表达式建议 - Regular Expression advice for [Alphanumeric][alphanumeric.-_@] 31 characters 正则表达式:仅限字母、数字、空格、连字符、句点和下划线,并且必须以字母开头 - Regular Expression: Letters, numbers, spaces, hyphens, periods, and underscores only and must begin with a letter 正则表达式只允许不会断开链接的字符 - Regular expression allowing only characters that will not break a link
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM