问题:要求使用js正则表达式检验输入框,除数字不能输入其他字符数据?
我测试图
这个是代码js正则式
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
asasasasasa中文
<form name="form" action="NewFile2.jsp" method="post">
<input type="text" id="num" name="num"/>
<input type="submit" name="aa1">
<input type="button" value="提交" onclick="inputNull(this)"/>
<% out.print(request.getParameter("num"));
if(request.getParameter("num")!=null)
for ( int i=0; i<Integer.parseInt(request.getParameter("num")); i++ ) {
%> 你好<br> <%
}
%>
</form>
<script type="text/javascript">
function inputNull(form) {
alert("sdsd");
var oTxt = document.getElementById("num").value;
alert(oTxt);
var reg =/^[0-9]{1,}$/;
if ( !reg.test(oTxt)) {
alert("只能输入数值的数据")
return false;
} else {
document.form.action = "NewFileI.jsp";
document.form.submit();
}
}
</script>
</body>
</html>