2011-02-25 77 views
1

我需要驗證文本框,它應該只接受字母(大寫或小),數字和.,?-_。 它不應該接受任何其他值。 它不應該接受相同的值,如......,,,,,-----????_____。 它不應該接受像,._這樣的值,它應該包含字母或數字,例如_.ab.?驗證在Javascript中使用正則表達式的輸入

對我來說這是迫切的要求。有人可以寄給我嗎?

+0

你能描述一下現場代表?這會讓你更清楚你想要捕捉什麼邏輯。 – 2011-02-25 09:56:57

+0

請顯示您嘗試過的一些特定代碼。如果你不知道從哪裏開始,谷歌「javascript正則表達式教程」 – Matt 2011-02-25 09:57:41

+5

嗯,「緊急」和「發送給我」... – Gumbo 2011-02-25 09:57:44

回答

2

所以你想要字符串包含至少一個(ASCII)字母數字字符和至少兩個不同的字符?

嘗試

/^(?=.*[A-Z0-9])(?!(.)\1*$)[A-Z0-9.,?_-]*$/i 

說明:

^    # start of string 
(?=.*[A-Z0-9]) # assert that there is at least one of A-Z or 0-9 
(?!(.)\1*$)  # assert that the string doesn't consist of identical characters 
[A-Z0-9.,?_-]* # match only allowed characters 
$    # until the end of the string