简体   繁体   English

Javascript表单验证无效

[英]Javascript form validation not working

I can't understand why my javascript isn't working... Do i need to declare a variable somewhere? 我无法理解为什么我的javascript无法工作...我需要在某处声明一个变量吗?

<script type="text/javascript">
function validation(form) {
      

if(form.first_name.value == '' ) {

alert('Please enter your first name');
     form.first_name.focus();
return false;
}


if(form.00N30000006S4uq.value == '') {

alert('Please enter the high end of your budget');
  form.company.focus();
return false;
}


return true;
}
</script>



<form action="https://www.salesforce.com/servlet/servlet.WebToLead" method="POST" onsubmit="return validation(this);">

Form names need to begin with a letter. 表格名称需要以字母开头。 "00N30000006S4uq" fails because it begins with a number. “00N30000006S4uq”失败,因为它以数字开头。

See: http://www.w3.org/TR/html401/types.html#type-cdata 请参阅: http//www.w3.org/TR/html401/types.html#type-cdata

As mentioned by @ReturnTrue, the NAME must begin with a letter. 正如@ReturnTrue所提到的, NAME必须以字母开头。 That is why your script is failing. 这就是你的脚本失败的原因。

In your case since the field is auto-generated, if you know the flow of the elements in the form then you can reference the form elements array, like this... 在你的情况下,因为字段是自动生成的,如果你知道表单中元素的流程,那么你可以引用表单元素数组,就像这样......

form.elements[2].value

where form.elements[2] is form.00N30000006S4uq . 其中form.elements[2]form.00N30000006S4uq That will do the job. 那将完成这项工作。

Example: 例:

function validation(form) {
  if(form.elements[0].value == '' ) {
      alert('Please enter your first name');
      form.first_name.focus();
      return false;
  }

  if(form.elements[2].value == '') {
      alert('Please enter the high end of your budget');
      form.company.focus();
      return false;
  }

  return true;
}

<form action="" method="POST" onSubmit="return validation(this);">
<input type="text" name="first_name" />
<input type="text" name="company" />
<input type="text" name="00N30000006S4uq" />
<input type="submit" name="submit" />
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM