Javascript中的表单验证

时间:2016-02-09 18:00:25

标签: javascript html validation

我似乎无法弄清楚当我点击提交按钮时,为什么我的代码不会给我一个错误或成功消息。我只是试图以html格式验证字符串,以确保它们不是空的,不是太大等等。

开发人员的工具和在线Lint给了我没有错误。希望你们能指出我写过的任何难以理解的代码。

Javscript:

function submit()
{
  var firstName = document.getElementById("fn");
  var lastName = document.getElementById("ln");
  var email = document.getElementById("email");
  var emailR = document.getElementById("emailR");
  var userName = document.getElementById("un");
  var password = document.getElementById("pass");
  var passwordR = document.getElementById("passR");
  var error = "";
  var letters = /^[a-zA-Z]+$/;
  var postal = document.getElementById("pc");
  var code = /^[A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d$/;

  if (firstName === null || firstName.length > 15 || !firstName.value.match(letters))
  {
    error += "Please enter a valid first name.";
  }
  if (lastName === null || lastName.length > 15 || !lastName.value.match(letters))
  {
    error += " Please enter a valid last name";
  }

  var ei = email.value.lastIndexOf('@');
  var dot = email.value.lastIndexOf('.');
  if (email === null || email.length < 7 || ei == -1 || dot == -1 || dot < ei + 2)
  {
    error += " Please enter a valid email address";
  }
  if (emailR != email)
  {
    error += " Your email addresses did not match";
  }
  if (postal === null || !postal.length == 7 || !postal.value.match(code))
  {
    error += "Postal code must follow Canadian standard. E.g. \"N3H 1M1\" ";
  }
  if (userName === null || userName.length > 15)
  {
    error += " Please enter a valid username (must be less than 15 characters)";
  }
  if (password === null || password.length > 15)
  {
    error += "please enter a valid password (must be less than 15 characters)";
  }
  if (passwordR != password)
  {
    error += "your passwords do not match";
  }
  if (error === "")
  {
    document.getElementById("test").innerHTML = "Thank you for signing up!";
  }
  else if (error !== "")
  {
    document.getElementById("test").innerHTML = error;
  }
}

谢谢, Javanoob

1 个答案:

答案 0 :(得分:3)

var array = collection.Select(() => .....).ToList(); while (index < array.Count()) { int index_dup = index this.CurrentDispatcher.Invoke(new Action(() => { ... object a = array.ElementAt(index_dup) ... }), DispatcherPriority.Input); index++; } 为您提供完整的HTMLElement对象。要获得它的价值,请使用它的document.querySelector('.action-submit').removeAttribute('disabled'); //or document.getElementsByClassName("action-submit")[0].removeAttribute('disabled'); 属性。例如:

//getting column id by comparing the id need to delete the data from that row. $id=$_GET['id']; $res = "DELETE rental_annual_rent, rental_block, rental_street, rental_area, rental_town, rental_state, rental_pincode FROM house_details WHERE house_details_id='$id'"; $result=mysql_query($res); if(mysql_affected_rows()){ echo "successfully deleted"; session_start(); header("Location:property.php"); }else{ echo "Failure"; }

(假设带有document.getElementById的HTML元素是文本输入)