javascript验证输入的是不是5到15的数字…… 帮看看那里出错啦…… 运行没反应呀!
<%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%><%Stringpath=request....
<%@ 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 'begin.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 checkNumber()
{
var number = document.getElementsByName("number")[0];
if(number.length < 1)
{
alert("请输入!");
number.focus();
return false;
}
for(var i=0; i<number.length; i++)
{
var chNo = "0123456789";
if(chNo.indexOf(number.charAt(i)) == -1)
{
alert("请输入数字!");
number.focus();
return false;
}
}
if(parseInt(number.value)<5 || parseInt(number.value)>15)
{
number.value = 10;
}
return true;
}
</script>
</head>
<body>
<form name="form1" onsubmit="return checkNumber();">
请输入5到15中的任意一个数:<br/>
<input type="text" name="number" /><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html> 展开
<%
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 'begin.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 checkNumber()
{
var number = document.getElementsByName("number")[0];
if(number.length < 1)
{
alert("请输入!");
number.focus();
return false;
}
for(var i=0; i<number.length; i++)
{
var chNo = "0123456789";
if(chNo.indexOf(number.charAt(i)) == -1)
{
alert("请输入数字!");
number.focus();
return false;
}
}
if(parseInt(number.value)<5 || parseInt(number.value)>15)
{
number.value = 10;
}
return true;
}
</script>
</head>
<body>
<form name="form1" onsubmit="return checkNumber();">
请输入5到15中的任意一个数:<br/>
<input type="text" name="number" /><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html> 展开
1个回答
展开全部
错在你一会儿用number当dom对象一会儿又它当成值。
修改后的代码:
<script type="text/javascript">
function checkNumber()
{
var number = document.getElementsByName("number")[0].value;
if(number.length < 1)
{
alert("请输入!");
number.focus();
return false;
}
for(var i=0; i<number.length; i++)
{
var chNo = "0123456789";
if(chNo.indexOf(number.charAt(i)) == -1)
{
alert("请输入数字!");
number.focus();
return false;
}
}
if(parseInt(number)<5 || parseInt(number)>15)
{
document.getElementsByName("number")[0].value = 10;
}
return true;
}
</script>
修改后的代码:
<script type="text/javascript">
function checkNumber()
{
var number = document.getElementsByName("number")[0].value;
if(number.length < 1)
{
alert("请输入!");
number.focus();
return false;
}
for(var i=0; i<number.length; i++)
{
var chNo = "0123456789";
if(chNo.indexOf(number.charAt(i)) == -1)
{
alert("请输入数字!");
number.focus();
return false;
}
}
if(parseInt(number)<5 || parseInt(number)>15)
{
document.getElementsByName("number")[0].value = 10;
}
return true;
}
</script>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询