HTML

<!DOCTYPE html>
<html>
<head>
	<title>验证邮箱合法性</title>
</head>
<body>
	<input type="text" id="mytext">
	<span style="font-size: 10px; color: blue;" id="myspan"></span><br><br>
	<input type="button" value="验证邮箱合法性" id="mybtn">
	<script type="text/javascript" src="email.js"></script>
</body>
</html>

JS

window.onload = function() {
	document.getElementById("mybtn").onclick = function() {
		var email = document.getElementById("mytext").value;
		var emailRegex = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
		var isLegal = emailRegex.test(email);
		if (isLegal) {
			document.getElementById("myspan").innerText = "合法";
		} else {
			document.getElementById("myspan").innerText = "不合法";
		}
	}
	document.getElementById("mytext").onfocus = function() {
		document.getElementById("myspan").innerText = "";
	}
}
分类: Web前端

Deng Jia

Keep Moving.

0 条评论

发表评论

电子邮件地址不会被公开。