<!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>Test</title>
<script type="text/javascript">
function checkText1()
{
var objRegExp = /[^\u4E00-\u9FA5|\w]+/;
var strText1 = document.getElementById("text1").value; if(objRegExp.test(strText1))
{
window.alert("含有非法字符!");
}
}
function checkText2()
{
var objRegExp1 = /[a-zA-Z]+\d+[a-zA-Z0-9]*|\d+[a-zA-Z]+[a-zA-Z0-9]*/;
var objRegExp2 = /[^a-zA-Z0-9]+/;
var strText2 = document.getElementById("text2").value;
if(objRegExp2.test(strText2))
{
window.alert("含有非法字符!");
return false;
} if(!objRegExp1.test(strText2) || strText2.length > 16 || strText2.length < 6)
{
window.alert("字符串必须为6到16,且包含数字和字母!");
return false;
}
}
</script>
</head><body>
第一项:<input type="text" id="text1" /><input type="button" value="检测" onclick="checkText1()" /><br/>
第二项:<input type="text" id="text2" /><input type="button" value="检测" onclick="checkText2()" />
</body>
</html>
不知道能不能满足要求,没想到更好的写法。
var reg1 = /^[\u4E00-\u9FA5\w]+$/;
var reg2 = /^(?=[a-z]+\d|\d+[a-z])[\da-z]{6,16}$/i;
alert(reg1.test("中文abcABC123_"));
alert(reg1.test(",.?@#。!"));alert(reg2.test("a1"));
alert(reg2.test("a1a1a1a1"));
alert(reg2.test("aaaaaaaa"));
alert(reg2.test("22222222"));
第二个:/^[a-zA-Z\d]{6,20}$/
if(s.test(/[^_a-zA-Z0-9\u4E00-\u9FA5]/)){
alert("input error!");
}(2)
if(s.match(/^(\w|\d){6,16}$/) && str.match(/\d+|[a-zA-Z]+/g).length > 1){
}else{
alert("input error!");
}
<script>
function one()
{
var one = document.getElementById("one").value; if(!/[\u4E00-\u9FA5\w]+$/.exec(one))
{
window.alert("含有非法字符!");
}
}
function two()
{
var two = document.getElementById("two").value; if(!/[a-zA-Z\d]{6,20}$/.exec(two))
{
window.alert("含有非法字符!");
}
}
</script>
<body>
<input type="text" id="one"><input type="button" value="检测" onclick="one();"><br/>
<input type="text" id="two"><input type="button" value="检测" onclick="two();">
</body>
</html>