2014-01-28 56 views
2

我試圖驗證文本輸入下列要求:的Javascript正則表達式輸入驗證,以防止重複字符

  1. 允許的字符&長度/^\w{8,15}$/

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

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

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

  5. 不得包含重複字符(即, aba=passaab=fail

  6. 當與.test()一起使用時,每個測試將返回true。

儘管熟悉程度不高,但我可以編寫前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 

最後,理想的情況是用於強制執行所有要求單個組合測試。

+0

你需要使用正則表達式?只是循環輸入字符串中的字符可能會更容易 –

+0

根據我的經驗,複雜的字符串測試通常最好使用RegEx完成。在這裏,設置2-4的標誌並記住5的前一個字符是促使我遠離單個字符比較的原因。 – nico

回答

0

這爲您的測試使用了負向和正向lookahead零長度斷言,並且.{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 
相關問題