我想使用正則表達式進行郵件驗證,但它不工作。哪裏是我的錯誤? 它通常工作,但是當我把一個正則表達式控制它從來沒有看到是你輸入的郵件是啓用這個正則表達式格式。郵件驗證與正則表達式
這是我的代碼;
的index.jsp
<head>
<title>Mail Control From Db</title>
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<script type="text/javascript" src="check.js"></script>
</head>
<body>
<div align="left">
<form name="chkForm" id="chkForm" method="post" >
<table>
<tr>
<td>Email</td>
<td>
<input type="text" name="email" id="email" size="20" ></td>
<td>
<div id="emailInfo" align="left"></div>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
check.js
$(document).ready(function()
{
var myForm = $("#chkForm"), email = $("#email"), emailInfo = $("#emailInfo");
//send ajax request to check email
email.blur(function()
{
$.ajax({
type: "POST",
data: "email="+$(this).attr("value"),
url: "check.jsp",
beforeSend: function()
{
emailInfo.html("<font color='blue'>Kontrol Ediliyor..</font>");
},//end beforeSend: function()
success: function(data)
{
var reg = /\[email protected]\S+\.\S+/;
if (reg.test(email.val()))
{
var checkData = data.toString();
if(checkData == 0)
{
emailok = false;
emailInfo.html("<font color='red'>Mail in use</font>");
}//end if(checkData == 0)
else if(checkData == 1)
{
emailok = true;
emailInfo.html("<font color='green'>Mail is not in use</font>");
}//end else if(checkData == 1)
}//end if (reg.test(email.val()))
else
{
emailInfo.html("<font color='red'>ınvalid mail</font>");
}//end else
}// end success: function(data)
});
});//end email.blur(function()
});//end $(document).ready(function()
我在check.jsp一個問題。並解決它。
- 問題是關於regex.Regex是錯誤的。
- 條件錯誤。我用if(reg.test(email.val()))改變它。
請將您的解決方案添加爲答案*並將其標記爲正確的答案。 – brasofilo
可能使用[使用正則表達式驗證電子郵件地址]的副本(http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address) – tripleee