廖雪峰JavaScript练习题3

请尝试写一个验证Email地址的正则表达式。版本一应该可以验证出类似的Email:

正则表达式:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
</head>
<body>
	<script type="text/javascript">
		var re = /^[a-z]{1,8}\.?[0-9]{0,5}[a-z]{0,6}\@[a-z]{0,10}[0-9]{0,4}\.[a-z]{3}/
		var i,success = true,should_pass = ['[email protected]', '[email protected]', '[email protected]', '[email protected]'],should_fail = ['test#gmail.com', 'bill@microsoft', 'bill%[email protected]', '@voyager.org'];
		for (i = 0; i < should_pass.length; i++) {
			if (!re.test(should_pass[i])) {
				console.log('测试失败: ' + should_pass[i]);
				success = false;
				break;
			}
		}
		for (i = 0; i < should_fail.length; i++) {
			if (re.test(should_fail[i])) {
				console.log('测试失败: ' + should_fail[i]);
				success = false;
				break;
			}
		}
		if (success) {
			console.log('测试通过!');
		}
	</script>
</body>
</html>

  

猜你喜欢

转载自www.cnblogs.com/hys-blog/p/10029313.html