更正错误后隐藏javascript innerHTML

时间:2016-10-17 06:44:50

标签: javascript

我使用以下javascript代码在表单中添加内联错误。我希望在纠正每个字段后纠正错误后删除错误。我为所有验证创建了一个函数,因此无法使用else并手动删除错误。

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])(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{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;
  }
}

2 个答案:

答案 0 :(得分: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])(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,}$/;

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

 //Add your if else here.
}

希望它会有用。 :)

答案 1 :(得分:0)

在elseif

之后提供额外的else语句
;WITH cte As (SELECT col1,col2, Row_number() over(partition by col3 order by id) as rowNum
FROM table) 

SELECT * FROM cte WHERE rowNum=1
SELECT * FROM cte WHERE rowNum >1 -- ERROR occurs as cte defined can be used only uptil next statement of cte definition done by using WITH clause

同样适用于else { document.getElementById('pointemail').innerHTML = ''; }