1. 程式人生 > >使用正則表達式和indexof驗證及識別郵箱類型

使用正則表達式和indexof驗證及識別郵箱類型

tle .get else if 進行 value ins har 163郵箱 panel

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
</head>
<body>
郵箱:<input type="text" placeholder="請輸入郵箱地址" name="mail"/><span id="span"></span>

<script>

var inputEle=document.getElementsByName("mail")[0];
var spanEle=document.getElementById("span");
var reg=/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
inputEle.onblur=function(){
//郵箱的非空驗證:
if(this.value.trim()==""){11
spanEle.style.color="red";
spanEle.innerHTML="郵箱不能為空";
//不為空則進行郵箱格式的驗證
}else if(reg.test(this.value)){//如果是爭取的格式,再進行類型的識別
if(this.value.indexOf("qq")!=-1){//如果值中含有qq則為qq郵箱
spanEle.style.color="green";
spanEle.innerHTML="當前郵箱為QQ郵箱";
}else if(this.value.indexOf("163")!=-1){
spanEle.style.color="green";
spanEle.innerHTML="當前郵箱為網易163郵箱";
}else if(this.value.indexOf("sina")!=-1){
spanEle.style.color="green";
spanEle.innerHTML="當前郵箱為新浪郵箱";
}else{
spanEle.style.color="red";
spanEle.innerHTML="未能識別的郵箱類型";
}
}else{//不為空但是格式不正確
spanEle.style.color="red";
spanEle.innerHTML="輸入的郵箱格式錯誤,請輸入正確的郵箱";
}

}
</script>
</body>
</html>

使用正則表達式和indexof驗證及識別郵箱類型