js的关于验证文本框中的文本是否都是数字

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'js8.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function validate()
{

var number = document.getElementsByName('num')[0];

/*if(number.value=='')
{
alert("不能为空");
number.focus();
return false;
}
for(var i = 0; i < number.value.length; i++)
{
var str = "0123456789";
if(str.indexOf(number.value.charAt(i))==-1)
{
alert("输入的不全是数字");
number.value = '';
number.focus();
return false;
}
}
if(parseInt(number.value) < 5 || parseInt(number.value) > 15)
{
number.value = 10;
}
return true;*/
//该方法不行:因为parseInt('12asd')会变成12是一个数字,显然与题意不符
alert(parseInt(number.value));
if(isNaN(parseInt(number.value)))
{
alert("非法数字");
return false;
}
return true;
}


</script>
</head>

<body>
<form action="js9.jsp" method="post" onsubmit="return validate();">
<input type="text" name="num">
<input type="submit" value="提交"/>
</form>
</body>
</html>

你可能感兴趣的:(文本框)