js正则表达式验证文本框的问题

来源:百度知道 编辑:UC知道 时间:2024/05/21 09:14:21
请问怎么才能使用正则验证一个文本框只能输入数字和英文或数字和英文的组合,如果输入其他的话会用alert提示

谢谢 急用...在线等 看了很多资料 但不会用

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
function to()
{
var reg=/\W/;
var x=document.form1.txt.value;
alert(x);
if(!reg.test(x))
{
alert("right!!");
}
else
{
alert("error");
}
}
</script>
</head>
<body>
<p>  </p>
<form name="form1" method="post" action="">
<input type="text" name="txt">
<input type="submit" onClick="to()"/>
</form>
</body>
</html>