Javascript正则表达式输入验证以防止重复字符

时间:2014-01-28 04:17:08

标签: javascript regex

我正在尝试使用以下要求验证文本输入:

  1. 允许使用字符&长度/^\w{8,15}$/

  2. 必须包含/[a-z]+/

  3. 必须包含/[A-Z]+/

  4. 必须包含/[0-9]+/

  5. 不得包含重复的字符(例如。aba=passaab=fail

  6. .test()一起使用时,每个测试都会返回true。

  7. 熟悉程度不高,我能够编写前4个测试,尽管是单独的。第五次测试没有成功,否定前瞻(这是我认为我需要使用的)是具有挑战性的。

    以下是一些价值/结果示例:

    re.test("Fail1");//returns false, too short
    
    re.test("StringFailsRule1");//returns false, too long
    
    re.test("Fail!");//returns false, invalid !
    
    re.test("FAILRULE2");//returns false, missing [a-z]+
    
    re.test("failrule3");//returns false, missing [A-Z]+
    
    re.test("failRuleFour");//returns false, missing [0-9]+
    
    re.test("failRule55");//returns false, repeat of "5"
    
    re.test("TestValue1");//returns true
    

    最后,理想的是用于强制执行所有要求的单一组合测试。

1 个答案:

答案 0 :(得分:0)

这为测试使用负向和正向前导零长度断言,.{8,15}位验证长度。

^(?!.*(.)\1)(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])\w{8,15}$

对于你的第五条规则,我使用了一个负面的先行,以确保任何角色的捕获组永远不会被它自己跟随。

Regexpal demo

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    (                        group and capture to \1:
--------------------------------------------------------------------------------
      .                        any character except \n
--------------------------------------------------------------------------------
    )                        end of \1
--------------------------------------------------------------------------------
    \1                       what was matched by capture \1
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    [a-z]                    any character of: 'a' to 'z'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    [A-Z]                    any character of: 'A' to 'Z'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    [0-9]                    any character of: '0' to '9'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  \w{8,15}                 word characters (a-z, A-Z, 0-9, _)
                           (between 8 and 15 times (matching the most
                           amount possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string