创建正则表达式对象的方法
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>DOM编程-关于正则表达式</title>
</head>
<body>
<script type="text/javascript">
window.onload = function(){
document.getElementById("btn").onclick = function(){
var email = document.getElementById("email").value;
var emailRegExp = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
var ok = emailRegExp.test(email);
if(ok){
document.getElementById("ee").innerText="邮箱地址合法";
}else{
document.getElementById("ee").innerText="邮箱地址不合法";
}
}
document.getElementById("email").onfocus = function(){
document.getElementById("ee").innerText = "";
}
}
</script>
<input type="text" id="email" />
<span id="ee" style="color:red; font-size: 12px;"></span>
<br>
<input type="button" value="验证邮箱" id="btn" />
</body>
</html>