繁体   English   中英

使用Javascript验证多个功能?

[英]Javascript validation for multiple functions?

我有一个表单的javascipt函数。 代码是:

<script type="text/javascript">
function verify() {
    if (isNaN(document.form1.exp_amount.value) == true) {
        alert("Invalid Block Amount");
        return false;
    } else if ((document.form1.exp_name.value).length == 0) {
        alert("Block Exp is left Blank!");
        return false;
    } else if ((document.form1.exp_amount.value).length == 0) {
        alert("Block Amount is left Blank!");
        return false;
    } else {
        document.form1.submit();
        return true;
    }
}
</script>

现在我必须为其提供字母验证,这在单独的JS函数中具有:

<script language="javascript" >
function checkName() {
    re = /^[A-Za-z]+$/;
    if (re.test(document.exp_name.form1.value)) {
        alert('Valid Name.');
    } else {
        alert('Invalid Name.');
    }
}
</script>

如果我想在函数verify()中进行字母验证,该怎么办? 还是还有其他方法?

请更改您的验证和表格,以便在有效时提交表格,否则将提交错误。 我认为以下代码是规范的,并且可以在所有支持正则表达式的浏览器上使用(1996年在JS1.1中以NS3.0引入)-请注意,除非您引用字段名称,否则javascript不支持名称中的破折号在脚本中。 该代码不需要命名表单,因为它在调用(此)中传递表单对象并将函数中的对象用作Form

<html>
<head>
<title>Canonical forms validation without jQuery</title>
<script type="text/javascript">
var validName = /^[A-Za-z]+$/;
function checkName(str) {
  return validName.test(str);
}

function verify(theForm) {
  // note: theForm["..."] is short for theForm.elements["..."];
  var amount = theForm["exp_amount"].value; 
  if(amount ==""){
    alert("Block Amount is left blank");
    theForm["exp_amount"].focus();
    return false;
  }
  if (isNaN(amount)) {
    alert("Invalid Block Amount");
    theForm["exp_amount"].focus();
    return false;
  }

  var name = theForm["exp_name"].value; 
  if(name.length==0) {
    alert("Block Exp is left Blank!");
    theForm["exp_name"].focus();
    return false;
  }
  if(!checkName(name)) {
    alert("Block Exp is invalid!");
    theForm["exp_name"].focus();
    return false;
  }
  return true;
}
</script>
</head>
<body>
<form onsubmit="return verify(this)">
Amount: <input type="text" name="exp_amount" value="" /><br />
Name: <input type="text" name="exp_name" value="" /><br />
<input type="submit" />
</form>
</body>
</html>
<script type="text/javascript">
function verify()
{
    if(isNaN(document.form1.exp_amount.value)==true)
    {
         alert("Invalid Block Amount");
         return false;
    }
    else if((document.form1.exp_name.value).length==0)
    {
         alert("Block Exp is left Blank!");
         return false;
    }
    else if((document.form1.exp_amount.value).length==0)
    {
         alert("Block Amount is left Blank!");
         return false;
    }
    else if(!(/^[A-Za-z]+$/.test(document.form1.exp_amount.value))) //ADD THIS
    {
        alert('Invalid Name');
        return false;
    }

     document.form1.submit();
     return true;
}
</script>

只需在您的checkName函数内返回false或true checkName

function checkName()
{
    re = /^[A-Za-z]+$/;
    if(re.test(document.exp_name.form1.value))
    {
        alert('Valid Name.');
        return true;
    }
    else
    {
        alert('Invalid Name.');
        false;
    }
}

然后调用它并检查结果。

    ...

    else if((document.form1.exp_amount.value).length==0)
    {
        alert("Block Amount is left Blank!");
        return false;
    }

    else if (!checkName()) {
        return false;
    }

    else
    {
        document.form1.submit();
        return true;
    }

顺便说一句,您可以采用多种方法来清理和改进您的代码。 我不想现在就进入其中,但是如果您想讨论它,请发表评论。

编辑您的checkName()函数以

function checkName()
{
re = /^[A-Za-z]+$/;
if(re.test(document.exp_name.form1.value))
{
     alert('Valid Name.');
     return true;
}
else
{
    alert('Invalid Name.');
    return false;

}

}

并添加

else if(!checkName()){ return false;}

在表单提交之前输入验证代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM