郵件正則表達式
一般的email,形如[email protected],[email protected]這樣一些常用的形式就行了,但是在我們公司的一些客戶中郵箱卻有一些[email protected],[email protected],[email protected]這 種類似的形式,在@符號之前還有
點.,原來是拿來就用,可是現在就不行,得自己研究正則的用法了
原來的正則表達式
/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
我根據我的實際情況修改後的正則表達式
/^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/;
或者
/^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/;
字符描述:
^ :匹配輸入的開始位置。
\:將下一個字符標記爲特殊字符或字面值。
* :匹配前一個字符零次或幾次。
+ :匹配前一個字符一次或多次。
(pattern) 與模式匹配並記住匹配。
x|y:匹配 x 或 y。
[a-z] :表示某個範圍內的字符。與指定區間內的任何字符匹配。
\w :與任何單詞字符匹配,包括下劃線。
{n,m} 最少匹配 n 次且最多匹配 m 次
$ :匹配輸入的結尾。
附一簡單的js
function checkEmail()
{
var emailValue=document. getElementById_r("email").value;
if (!isEmail(emailValue))
{
alert("您輸入的郵箱有誤,請重新覈對後再輸入!");
document. getElementById_r("email").focus();
return false;
}
return true;
}
function isEmail(str){
var reg = /^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/;
return reg.test(str);
}
輸入多個郵件時的驗正方法
//驗正郵箱格式要正確 20080602_heyitang
var email=document. getElementById_r("trans_email").value;
//如果,用戶入了郵箱才需要進行判斷
if (email!=null)
{if (email.indexOf(";")==-1)
{
if(!isEmail(email))
{
alert("您輸入的單個郵件格式有誤,請重新覈對後再輸入");
document. getElementById_r("trans_email").focus();
return false;
}
}
else
{
var emailArray=email.split(";");
for(i=0;i<emailArray.length;i++)
{
//這裏防止出現[email protected];[email protected];;多加了;這時候,數組中可能有元素沒有內容
if(emailArray[i]!=null || emailArray[i]!="")
{
if(!isEmail(emailArray[i]))
{
alert("您輸入的多個郵箱格式中有郵箱格式不 正確,請重新覈對後再輸入");
document. getElementById_r("trans_email").focus();
return false;
}
}
}
}
}