function checkDigitalChar(cntrl,errmsg3,flag,errmsg1,errmsg2)
{
var str;
str = cntrl.value;
if (str.indexOf(" ") >=0)
{
window.alert(errmsg3);
cntrl.focus();
cntrl.select();
return false;
}
if (flag == 1){
if(str.length==0)
{
//alert("Input is blank, please re-input.");
alert(errmsg1);
return false;
}
}
for(var loop_index=0; loop_index<str.length; loop_index++)
{
if(str.charAt(loop_index) == '0'
||str.charAt(loop_index) == '1'
||str.charAt(loop_index) == '2'
||str.charAt(loop_index) == '3'
||str.charAt(loop_index) == '4'
||str.charAt(loop_index) == '5'
||str.charAt(loop_index) == '6'
||str.charAt(loop_index) == '7'
||str.charAt(loop_index) == '8'
||str.charAt(loop_index) == '9')
continue;
else
{
//alert("Valid characters are 0-9. Please re-input.");
alert(errmsg2);
return false;
}
}
return true;
}
{
var str;
str = cntrl.value;
if (str.indexOf(" ") >=0)
{
window.alert(errmsg3);
cntrl.focus();
cntrl.select();
return false;
}
if (flag == 1){
if(str.length==0)
{
//alert("Input is blank, please re-input.");
alert(errmsg1);
return false;
}
}
for(var loop_index=0; loop_index<str.length; loop_index++)
{
if(str.charAt(loop_index) == '0'
||str.charAt(loop_index) == '1'
||str.charAt(loop_index) == '2'
||str.charAt(loop_index) == '3'
||str.charAt(loop_index) == '4'
||str.charAt(loop_index) == '5'
||str.charAt(loop_index) == '6'
||str.charAt(loop_index) == '7'
||str.charAt(loop_index) == '8'
||str.charAt(loop_index) == '9')
continue;
else
{
//alert("Valid characters are 0-9. Please re-input.");
alert(errmsg2);
return false;
}
}
return true;
}
<p><input type="text" name="T1" size="20"></p>
<p><input type="text" name="T2" size="20"></p>
<p><input type="text" name="T3" size="20"></p>
<p><input type="submit" value="提交" name="B1"></p>
</form>
<script language=JavaScript>
function check(){
var t1 = document.forms[0].t1;
if (checkDigitalChar(t1,"输入数时不允许有空格。",0,"数不能为空","必须是数字") ==false) {
t1.select();
return false;
}
var t2 = document.forms[0].t2;
if (checkDigitalChar(t2,"输入数时不允许有空格。",0,"数不能为空","必须是数字") ==false) {
t2.select();
return false;
}
var t3 = document.forms[0].t3;
if (checkDigitalChar(t3,"输入数时不允许有空格。",0,"数不能为空","必须是数字") ==false) {
t3.select();
return false;
}
}
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>无标题文档</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language=JavaScript>
function check(){
var t1 = document.forms[0].t1;
if (checkDigitalChar(t1,"输入数时不允许有空格。",0,"数不能为空","必须是数字") ==false) {
t1.select();
return false;
}
var t2 = document.forms[0].t2;
if (checkDigitalChar(t2,"输入数时不允许有空格。",0,"数不能为空","必须是数字") ==false) {
t2.select();
return false;
}
var t3 = document.forms[0].t3;
if (checkDigitalChar(t3,"输入数时不允许有空格。",0,"数不能为空","必须是数字") ==false) {
t3.select();
return false;
}
function checkDigitalChar(cntrl,errmsg3,flag,errmsg1,errmsg2)
{
var str;
str = cntrl.value;
if (str.indexOf(" ") >=0)
{
window.alert(errmsg3);
cntrl.focus();
cntrl.select();
return false;
}
if (flag == 1){
if(str.length==0)
{
//alert("Input is blank, please re-input.");
alert(errmsg1);
return false;
}
}
for(var loop_index=0; loop_index<str.length; loop_index++)
{
if(str.charAt(loop_index) == '0'
||str.charAt(loop_index) == '1'
||str.charAt(loop_index) == '2'
||str.charAt(loop_index) == '3'
||str.charAt(loop_index) == '4'
||str.charAt(loop_index) == '5'
||str.charAt(loop_index) == '6'
||str.charAt(loop_index) == '7'
||str.charAt(loop_index) == '8'
||str.charAt(loop_index) == '9')
continue;
else
{
//alert("Valid characters are 0-9. Please re-input.");
alert(errmsg2);
return false;
}
}
return true;
}
}
</script>
</head><body><form method="post" onsubmit="return check();">
<p><input type="text" name="t1" size="20"></p>
<p><input type="text" name="t2" size="20"></p>
<p><input type="text" name="t3" size="20"></p>
<p><input type="submit" value="提交" name="B1"></p>
</form></body>
</html>
提示错误:行5字符5,value为空或不是对象<script language=javascript>
function checkDigitalChar(cntrl,errmsg3,flag,errmsg1,errmsg2)
{
var str;
str = cntrl.value;
if (str.indexOf(" ") >=0)
{
window.alert(errmsg3);
cntrl.focus();
cntrl.select();
return false;
}
if (flag == 1){
if(str.length==0)
{
//alert("Input is blank, please re-input.");
alert(errmsg1);
return false;
}
}
for(var loop_index=0; loop_index<str.length; loop_index++)
{
if(str.charAt(loop_index) == '0'
||str.charAt(loop_index) == '1'
||str.charAt(loop_index) == '2'
||str.charAt(loop_index) == '3'
||str.charAt(loop_index) == '4'
||str.charAt(loop_index) == '5'
||str.charAt(loop_index) == '6'
||str.charAt(loop_index) == '7'
||str.charAt(loop_index) == '8'
||str.charAt(loop_index) == '9')
continue;
else
{
//alert("Valid characters are 0-9. Please re-input.");
alert(errmsg2);
return false;
}
}
return true;
}
function check(){
var t1 = document.forms[0].t1;
if (checkDigitalChar(t1,"输入数时不允许有空格。",0,"数不能为空","必须是数字") ==false) {
t1.select();
return false;
}
var t2 = document.forms[0].t2;
if (checkDigitalChar(t2,"输入数时不允许有空格。",0,"数不能为空","必须是数字") ==false) {
t2.select();
return false;
}
var t3 = document.forms[0].t3;
if (checkDigitalChar(t3,"输入数时不允许有空格。",0,"数不能为空","必须是数字") ==false) {
t3.select();
return false;
}
}
</script>
<form method="post" onsubmit="return check();">
<p><input type="text" name="T1" size="20"></p>
<p><input type="text" name="T2" size="20"></p>
<p><input type="text" name="T3" size="20"></p>
<p><input type="submit" value="提交" name="B1"></p>
</form>