JavaScript验证无法正常工作

JavaScript验证无法正常工作

它有什么问题为什么不起作用...

 <script language="JavaScript" type="text/javascript">


//function to check empty fields

function isEmpty(strfield1, strfield2) {
  //change "field1, field2 and field3" to your field names

  strfield1 = document.forms[0].name.value
  strfield2 = document.forms[0].email.value

  //name field

  if (strfield1 == "" || strfield1 == null || !isNaN(strfield1) || strfield1.charAt(0) == ' ') {
    alert( "Name is a mandatory field.\nPlease amend and retry.")
    return false;
  }

  //EMAIL field
  if (strfield2 == "" || strfield2 == null || !isNaN(strfield2) || strfield2.charAt(0) == ' ') {
    alert(" Email is a mandatory field.\nPlease amend and retry.")
    return false;
  }
  return true;
}


//function to check valid email address

function isValidEmail(strEmail){
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
  strEmail = document.forms[0].email.value;

 // search email text for regular exp matches

  if (strEmail.search(validRegExp) == -1)  {
    alert('A valid e-mail address is required.\nPlease amend and retry');
    return false;
  }
  return true;
}


//function that performs all functions, defined in the onsubmit event handler

function check(form)){
  if (isEmpty(form.field1)){
    if (isEmpty(form.field2)){
      if (isValidEmail(form.email)){
        return true;
      }
    }
  }
}
return false;
}

</script>


它不做任何事情,我不明白那里发生了什么,我也把它放在形式上

<form onsubmit="return check(this);" action="sendquery.php" name="contquery">

最佳答案

乍一看:@FishBasketGordo所示的括号过多,因此我不再重复

乍一看-您通过了字段但不测试字段值

第三眼:您没有将正确的名称传递给函数

第四眼-isEmpty为空时返回false。它应该返回true

我修好了所有这些

DEMO HERE

完整页面以显示行进路线。更新为在表单上进行不显眼的事件处理

<html>
<head>
<title>Validation</title>
<script type="text/javascript">

// trim for IE
if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
  }
}

//function to check empty fields

function isEmpty(objfld) {
  var val = objfld.value;
  if (val.trim() == "" || val == null) {
    alert(objfld.name+" is a mandatory field.\nPlease amend and retry.");
    objfld.focus();
    return true;
  }
  return false;
}

//function to check valid email address

function isValidEmail(objEmail){
  var validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
  var strEmail = objEmail.value;
  if (strEmail.match(validRegExp)) return true;
  alert('A valid e-mail address is required.\nPlease amend and retry');
  objEmail.focus();
  return false;
}

//function that performs all functions, defined in the onsubmit event handler

function validate(form) {
  if (isEmpty(form.name)) return false;
  if (isEmpty(form.email)) return false;
  return isValidEmail(form.email);
}


window.onload=function() {
  document.getElementById("form1").onsubmit=function() {
    return validate(this);
  }
}

</head>
<body>
<form id="form1">
  Name:<input type="text" name="name" /><br/>
  Email:<input type="text" name="email" /><br/>
  <input type="submit" />
</form>
</body>
</html>

关于javascript - JavaScript验证无法正常工作?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10582226/

10-11 12:42