javascript使用正则表达式验证Email地址

时间:2020-12-19 05:04:16
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>测试:用Javascript正则表达式验证Email地址</title>
</head>
<body style="background:#fff;">
<p style="margin-bottom:20px;">
输入Email地址,点击测试,错误的地址将返回 false...正确的,显示成? 自己猜:
</p>
<input type="text" value="" id="mail"/>
<input type="button" value="测试" id="checkout" onclick="isValidMail(sText)" />
<script type="text/javascript">
var sText = document.getElementById('mail').value;
document.getElementById('mail').onblur = function sValue(){
sText = this.value;
} function isValidMail(sText) {
var reMail = /^(?:[a-zA-Z0-9]+[_\-\+\.]?)*[a-zA-Z0-9]+@(?:([a-zA-Z0-9]+[_\-]?)*[a-zA-Z0-9]+\.)+([a-zA-Z]{2,})+$/;
alert(reMail.test(sText))
}
</script>
</body>
</html>