2015-09-29 25 views
0

守則,給我的錯誤信息,但在進入現場後不清除錯誤消息後不清除錯誤消息:守則,給我的錯誤信息,但進入現場

var flag = 0; 
function otpValidate() { 
    otp = oneTimePass.onetimepass.value; 
    if(otp == "") { 
     document.getElementById('error0').innerHTML = "Enter one time password"; 
     flag = 1; 
    } else if(otp.length != 6) { 
     document.getElementById('error0').innerHTML = "PIN must be 6 digits"; 
     flag = 1; 
    } 
} 
function check(form) { 
    flag = 0; 
    otpValidate(); 
    if (flag == 1) 
     return false; 
    else 
     return true; 
} 

回答

0

嘗試像這

var flag = 0; 
function otpValidate() { 
    otp = oneTimePass.onetimepass.value; 
    if(otp == "") { 
     document.getElementById('error0').innerHTML = "Enter one time password"; 
     flag = 1; 
    } else if(otp.length != 6) { 
     document.getElementById('error0').innerHTML = "PIN must be 6 digits"; 
     flag = 1; 
    } 
    else { 
     document.getElementById('error0').innerHTML = ""; 
     flag = 0; 
    } 
} 
function check(form) { 
    otpValidate(); 
    if (flag == 1) 
     return false; 
    else 
     return true; 
} 
0

首先在這裏修復代碼

function check(form) { 
    //flag = 0; This will make no sense to check it 

    otpValidate(); 
    if (flag == 1) 
     { 
     flag = 0; 
     return false; 
     } 

    else 
     return true; 
} 

而對於這個問題,你所面臨的

function otpValidate() { 
    otp = oneTimePass.onetimepass.value; 
    if(otp == "") { 
     document.getElementById('error0').innerHTML = "Enter one time password"; 
     flag = 1; 
    } else if(otp.length != 6) { 
     document.getElementById('error0').innerHTML = "PIN must be 6 digits"; 
     flag = 1; 
    } 
    else { 
     document.getElementById('error0').innerHTML = ""; 
    } 
} 
1

就在你的主函數中添加一個空的消息。然後調用一個keyup函數來調用你的主函數。試着這麼做:

function otpValidate() { 
    var otp = oneTimePass.onetimepass.value.trim(); 
    if(otp == "") { 
     document.getElementById('error0').innerHTML = "Enter one time password"; 
     flag = 1; 
    } else if(otp.length != 6) { 
     document.getElementById('error0').innerHTML = "PIN must be 6 digits"; 
     flag = 1; 
    } 
    else { 
     document.getElementById('error0').innerHTML = ""; 
     flag = 0; 
    } 
} 

var otp = oneTimePass.onetimepass; 
otp.addEventListener("keyup", otpValidate); 
1

優化的方式

function otpValidate() { 
    otp = oneTimePass.onetimepass.value.trim(); 
    document.getElementById('error0').innerHTML = ""; 
    flag = 0; 
    if(!otp) { 
     document.getElementById('error0').innerHTML = "Enter one time password"; 
     flag = 1; 
    } else if(otp.length != 6) { 
     document.getElementById('error0').innerHTML = "PIN must be 6 digits"; 
     flag = 1; 
    } 
    return flag; 
} 
function check(form) { 

    var flag = otpValidate(); 
    if (flag == 1) 
     return false; 
    return true; 
}