验证失败时退出javascript代码

时间:2013-07-15 09:42:47

标签: javascript

验证失败时,我需要帮助才能退出JavaScript代码。目前,当验证失败但JavaScript代码继续运行时,我收到正确的错误消息。请在下面找到我的代码。感谢

var CompPlanID=1;
var Component=2;
var TierNo=3;
var StartDate=4;
var EndDate=5;
var TierMin=6;
var TierMax=7;
var Rate=8;
var InvalidFlag = 0;
var BlankTextBox = '';

function DateCheck()
{
    var StartDateform= document.getElementById('tblTarget').rows[1].cells[StartDate].getElementsByTagName('input')[0].value;

    var EndDateform= document.getElementById('tblTarget').rows[1].cells[EndDate].getElementsByTagName('input')[0].value;

    var eDate = new Date(EndDateform);
    var sDate = new Date(StartDateform);

    if(StartDateform== BlankTextBox || EndDateform == BlankTextBox || sDate> eDate)
    {
        alert("Please ensure that the End Date is greater than or equal to the Start Date.");
    InvalidFlag = 1;
        }   
}

//检查pk行是否为空

function CheckPkRow()
{
    var CompPlanIDform= document.getElementById('tblTarget').rows[1].cells[CompPlanID].getElementsByTagName('select')[0].value;

  if(CompPlanIDform== BlankTextBox)
  {
    alert("Please ensure that the primary key is not empty");
    InvalidFlag = 1;

  }

}

function Submit() 
{

    InvalidFlag = 0;
    CheckPkRow();
    DateCheck();    

//如果验证为真,则调用submit函数。

    if(InvalidFlag == 0 )
    {

    $('button_submit').click(); 
    alert('The new rate submitted');

    }

}

2 个答案:

答案 0 :(得分:2)

function CheckPkRow()
{
    var CompPlanIDform= document.getElementById('tblTarget').rows[1].cells[CompPlanID].getElementsByTagName('select')[0].value;

  if(CompPlanIDform== BlankTextBox)
  {
    alert("Please ensure that the primary key is not empty");
    InvalidFlag = 1;
    return false;
  }
}

答案 1 :(得分:0)

将您的代码更改为

function DateCheck()
{
    var StartDateform= document.getElementById('tblTarget').rows[1].cells[StartDate].getElementsByTagName('input')[0].value;
    var EndDateform= document.getElementById('tblTarget').rows[1].cells[EndDate].getElementsByTagName('input')[0].value;
    var eDate = new Date(EndDateform);
    var sDate = new Date(StartDateform);
    if(StartDateform== BlankTextBox || EndDateform == BlankTextBox || sDate> eDate)
    {
        alert("Please ensure that the End Date is greater than or equal to the Start Date.");
        return false;
    }  
    return true;        
}

function CheckPkRow()
{
    var CompPlanIDform= document.getElementById('tblTarget').rows[1].cells[CompPlanID].getElementsByTagName('select')[0].value;
    if(CompPlanIDform== BlankTextBox)
    {
        alert("Please ensure that the primary key is not empty");
        return false;
    }
    return true;
}

function Submit() 
{
    if(CheckPkRow() &&  DateCheck())
    {
        $('button_submit').click(); 
        alert('The new rate submitted');
    }
}