JavaScript验证各种表单元素

时间:2014-10-22 19:19:48

标签: javascript

我有一个尝试验证某些表单元素的脚本但是我无法找到每个“if”我需要的正确语法,它绝对没有做任何事情。我相信问题可能是在同一个表单上有很多“标签”或其他形式,所以我可能要写这个元素所属的形式,但我不知道它的语法,就像它的 var NewStatus = document.getElementById(“ChangeStatusForm.CURRENT_STATUS”)。VALUE; 如果没有任何警报适用,那么我只需要它提交表格

<script type="text/javascript">
function ValidateInfo()


var NewStatus = document.getElementById("CURRENT_STATUS").VALUE;
var OldStatus = document.getElementByID("ovpInfo.current_status").VALUE;
var CheckNumber = document.getElementByID("check_number").VALUE;
var CheckDate = document.getElementByID("check_date").VALUE;


if (NewStatus == 0){
alert ("Please select a New Status");
    }
 else if (NewStatus == 5){
alert ("This claim has already been collected");
    } else if (NewStatus == 10){
alert ("This claim has already been collected");
    } else if (NewStatus == 10 and CheckNumber =="" or CheckDate == ""){
alert ("Please enter Payment Details");
    else
document.forms['ChangeStatusForm'].submit() ;
    }




</script>

为了得到至少一个变量检查​​工作我编辑了我的脚本。这不起作用(使用我发现的一些语法来指定元素所属的实际形式)

<script type="text/javascript">
function ValidateInfo()
{

var NewStatus = document.forms["ChangeStatusForm"]["CURRENT_STATUS"].value;


if (NewStatus == 0){
alert ("Please select a New Status");
        }
    }

</script>

1 个答案:

答案 0 :(得分:0)

你正在使用错误的表达式&#34; document.getElementById(&#34; id&#34;)&#34;并且您需要更改或,以及&#34;&amp;&amp;&#34;,&#34; ||&#34;。

我使用一个标志来返回正确的值。

例如:

  function ValidateInfo()

 var flag = true;
 var NewStatus = document.getElementById("CURRENT_STATUS").value;
 var OldStatus = document.getElementByID("ovpInfo.current_status").value;
 var CheckNumber = document.getElementByID("check_number").value;
 var CheckDate = document.getElementByID("check_date").value;


 if (NewStatus == 0){
  alert ("Please select a New Status");
  flag=false;
 }
 else if (NewStatus == 5){
 alert ("This claim has already been collected");
    flag=false;
  } else if (NewStatus == 10){
  alert ("This claim has already been collected");
     flag=false;
    } else if (NewStatus == 10 and CheckNumber =="" or CheckDate == ""){
    alert ("Please enter Payment Details");
       flag=false;


      }else{
        flag=true; 
            }
     if( flag ){document.forms['ChangeStatusForm'].submit()}
祝你好运