限制文本框的输入字符,只能输入字母,数字,下划线。
<input type="text" width="50px" id="txt" />
<input type="button" value="检测" onclick="yan()"/>
<script>
function yan()
{
var obj=document.getElementById("txt").value;
reg=new RegExp("^\w+$");
if(reg.test(obj)==false)
{
alert("不合法");
}
else
{
alert("合法");
}
}
</script>
这样为什么错??急救!!!!!!
<input type="text" width="50px" id="txt" />
<input type="button" value="检测" onclick="yan()"/>
<script>
function yan()
{
var obj=document.getElementById("txt").value;
reg=new RegExp("^\w+$");
if(reg.test(obj)==false)
{
alert("不合法");
}
else
{
alert("合法");
}
}
</script>
这样为什么错??急救!!!!!!
为什么里面是:reg.test
构造函数里要转义,你可以使用
/^\w+$/.test(obj);