2013-07-15 130 views
0

驗證失敗時,我需要幫助才能退出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行不爲空

//調用如果驗證爲真時提交功能。

if(InvalidFlag == 0) 
    { 

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

    } 

} 
+0

使用'返回false;' –

+0

使用返回false; –

+0

除了'return false;'檢查函數的返回值('if(!CheckPkRow())return false;') – Zeta

回答

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'); 
    } 
} 
+0

非常感謝Satpal Works –

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; 
    } 
}