我想驗證HTML格式的電子郵件地址。我對正則表達式知之甚少。javascript正則表達式 - 我的非常簡單的正則表達式有什麼不對?
驗證非常簡單,只是爲了匹配[email protected]_symbols.two_to_eight_symbols
模式。
這是我試圖使用的正則表達式^[email protected]+\..{2,8}$
。但它不起作用,它驗證模式[email protected]_symbols
。
注意:不要擔心這種簡單的驗證,在服務器端我正在做filter_var
(php)並將令牌發送到該電子郵件。只需要啓用表單按鈕時inputed電子郵件地址適合一些穩健型:)
編輯這些模式「any_symbols ......」我在問題中提到的是什麼,我想剛纔輸入的文字表述。這不是我在輸入字段中鍵入:)通常我輸入 「[email protected]」,或 「[email protected]」 等:)
EDIT2實際工作代碼:
var email_regex = new RegExp("^[email protected]+\..{2,8}$");
if ($target.val().match(email_regex) !== null){
$button.removeAttr('disabled').removeClass('disabled');
}
else{
$button.attr('disabled', 'disabled').addClass('disabled');
}
EDIT3 * 發現問題了! *這不是正則表達式本身,這是我如何傳遞的正則表達式的正則表達式的功能...它應該是
new RegExp(/^[email protected]+\..{2,8}$/);
不是
new RegExp("^[email protected]+\..{2,8}$");
正如我已經說過這整個事情的正則表達式是相當新的給我:))
在我的瀏覽器,'/^[email protected]+\..{2,8}$/.test("[email protected]_symbols 「)'是'false': - ? – 2012-04-25 14:40:20
嘗試''[email protected]_symbols'.match('^。+ @。+ \ .. {2,8} $');' – egis 2012-04-25 14:48:37
更新我的問題。同樣根據https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/match「如果沒有匹配,該方法返回'null'。」。 – egis 2012-04-25 14:56:33