2016-10-17 55 views
0

我已經使用以下JavaScript代碼在我的表單中添加內聯錯誤。我想在每個字段更正後糾正錯誤後刪除錯誤。我爲所有驗證創建了一個函數,因此無法使用else並手動刪除錯誤。糾正錯誤後隱藏javascript innerHTML

var emailpattern = /^[a-zA-Z][a-zA-Z0-9_]*(\.[a-zA-Z0-9_]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.([a-zA-Z]{2,4})$/ 

var passwordpattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[[email protected]$!%*?&])[A-Za-z\[email protected]$!%*?&]{8,}$/; 

function validateForm() { 
    var x = document.forms["LoginForm"]["email"]; 
    if (x.value == "") { 
    x.value = ""; 
    document.getElementById('pointemail').innerHTML = "Please enter the email id."; 
    x.focus(); 
    return false; 
    } else if (!emailpattern.test(x.value)) { 
    x.value = ""; 
    document.getElementById('pointemail').innerHTML = "Please enter a valid email address."; 
    x.focus(); 
    return false; 
    } 
    x = document.forms["LoginForm"]["password"]; 
    if (x.value == "") { 
    x.value = ""; 
    document.getElementById('pointpassword').innerHTML = "Please enter the password."; 
    x.focus(); 
    return false; 
    } else if (!passwordpattern.test(x.value)) { 
    x.value = ""; 
    document.getElementById('pointpassword').innerHTML = "Password should have minimum 8 characters, one upper case, one lower case and one special character"; 
    x.focus(); 
    return false; 
    } 
} 
+0

就在年底有效狀態 – Rajesh

回答

1

嘗試這個

var emailpattern = /^[a-zA-Z][a-zA-Z0-9_]*(\.[a-zA-Z0-9_]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.([a-zA-Z]{2,4})$/ 

var passwordpattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[[email protected]$!%*?&])[A-Za-z\[email protected]$!%*?&]{8,}$/; 

function validateForm() { 
    var x = document.forms["LoginForm"]["email"]; 
    document.getElementById('pointemail').innerHTML = ""; 
    document.getElementById('pointpassword').innerHTML = ""; 

//Add your if else here. 
} 

希望這將是有益的。 :)

+0

添加'else'塊,如果你不想使用這將是有益的其他。它會先空白,然後檢查。 –

+0

你正在運行它的變化事件? –

+1

哦!我的錯。沒有閱讀評論... – Rajesh

0

提供額外的else語句後ELSEIF

else { 
    document.getElementById('pointemail').innerHTML = ''; 
} 

同樣,對於pointPassword