一个循环,产生了类似这样的文本框
<input name="text(n)" type="text" id="text(n)" />
其中(n)为1往上递增的数字在表单提交前我想做个验证就是使得这些输入的只能是数字,可以是小数,为其他值或者为空都要提醒用户,请问该如何做?
谢谢
<input name="text(n)" type="text" id="text(n)" />
其中(n)为1往上递增的数字在表单提交前我想做个验证就是使得这些输入的只能是数字,可以是小数,为其他值或者为空都要提醒用户,请问该如何做?
谢谢
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<script language="javascript">
function checkForm() {
var obj=document.getElementsByTagName("input");
for (var i=0;i <obj.length;i++){
if (obj[i].type == "text" ) {
if (obj[i].value =="" ) {
alert("不能为空");
return false;
}
if (isNaN(obj[i].value)) {
alert("只能是数值");
return false;
}
}
}
return true;
}
</script><form name="form1" onsubmit="return checkForm()">
<input type="text" id="aa1"/><br/>
<input type="text" id="aa2"/><br/>
<input type="text" id="aa3"/><br/>
<input type="text" id="aa4"/><br/>
<input type="text" id="aa5"/><br/>
<input type="submit" value="提交"/><br/>
</form></body>
</html>
getElementsByTagName("input")
这样会不会from中 所有input都用这个验证呢?我还有其他 input 的。
function checkForm() {
var obj=document.getElementsByTagName("input");
for (var i=0;i <obj.length;i++){
if (obj[i].type == "text" && obj[i].name.substring(0,2) == "aa") {
if (obj[i].value =="" ) {
alert("不能为空");
return false;
}
if (isNaN(obj[i].value)) {
alert("只能是数值");
return false;
}
}
}
return true;
}
</script><form name="form1" onsubmit="return checkForm()">
<input type="text" id="aa1" name="aa1"/><br/>
<input type="text" id="aa2" name="aa2"/><br/>
<input type="text" id="aa3" name="aa3"/><br/>
<input type="text" id="ccc" name="ccc"/><br/>
<input type="text" id="aa4" name="aa4"/><br/>
<input type="text" id="aa5" name="aa5"/><br/>
<input type="submit" value="提交"/><br/>
</form>