葫芦金刚

导航

js 正则表达式,匹配邮箱/手机号/用户名

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>邮箱匹配</title>
</head>
<body>
	手机号:<input type="text" id="texts"><br \>
	邮箱:<input type="text" id="shouji" onblur='func(this)'>
	<script type="text/javascript">
	    //获取输入的手机号
		var texts = document.getElementById('texts');
		//获取输入的邮箱
		var shouji1 = document.getElementById('shouji');

		//手机号输入框失去焦点
		texts.onblur = function(){
			var tv = this.value;
			var reg = /^1[345789]\d{9}$/;
			if (reg.test(tv)){
				alert('Yes')
			}else{
				alert('No')
			}
		}
		//console.log(str);
		//邮箱输入框失去焦点
		function func(obj){
			var tv = obj.value;
			var reg = /^\w+\@+[0-9a-zA-Z]+\.(com|com.cn|edu|hk|cn|net)$/;
			if(reg.test(tv)){
				alert('邮箱格式正确');
			}else{
				alert('邮箱格式不正确')
			}
		}

		var as = '<a href="http://www.sina.com.cn">sina</a>'
		var reg = /<a href="(.*)">(.*)<\/a>/;
		var str = reg.exec(as);
		console.log(str);
	</script>
</body>
</html>

  

posted on 2017-12-26 19:37  葫芦金刚  阅读(4175)  评论(0编辑  收藏  举报