javascript验证不能包含特殊符号

用到的知识点:这里用的test()方法。

<!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>特殊字符验证</title>
</head>
<script>
function check(){
//不能不包含特殊符号
var re=/^([\u4E00-\u9FA5]|\w)*$/;
if(re.test(document.getElementById("myAttr").value)){
		//执行你的东东
	}
	else{
		alert("不能不包含特殊符")
	}
//附加:只能包含中文 /^([\u4E00-\u9FA5])*$/	
}

</script>
<body>
<form method=post action="" name="demo" id="demo" >
<input type="text" name="myAttr" id="myAttr">
<input type="button" onclick="check()"  value="提交">
</form>
</body>
</html>

金牌狙击手

52html5是一个web前端|html5资源平台,为广大html5开发者及爱好者提供html5相关的教程、资讯、html5游戏、html5教程等,并涉及css3、javascript前端知识。