简体   繁体   English

服务器/本地上的 JavaScript 表单验证

[英]JavaScript form validation on server/local

I'm using JavaScript form validation for the entry form for a contest I'm running.我正在为我正在举办的比赛的参赛表格使用 JavaScript 表单验证。 It's inline CSS so that if certain conditions aren't met, it displays, in red, messages that say "please enter your email address" or "that doesn't look like a valid email address" etc.它是内联 CSS,因此如果不满足某些条件,它会以红色显示消息,内容为“请输入您的电子邮件地址”或“这看起来不是有效的电子邮件地址”等。

The script, which sits at the top of the file, looks like this:位于文件顶部的脚本如下所示:

<script>

function checkForm() {
  name = document.getElementById("name").value;
  email = document.getElementById("email").value;
  terms = document.getElementById("terms").value;

  if (name == "") {
    hideAllErrors();
    document.getElementById("nameError").style.display = "inline";
    document.getElementById("name").select();
    document.getElementById("name").focus();
    return false;
  } else if (email == "") {
    hideAllErrors();
    document.getElementById("emailError").style.display = "inline";
    document.getElementById("email").select();
    document.getElementById("email").focus();
    return false;
  } 

  else if (!check_email(document.getElementById("email").value)) {
    hideAllErrors();
    document.getElementById("emailError2").style.display = "inline";
    document.getElementById("email").select();
    document.getElementById("email").focus();
    return false;
  } 

  else if (!document.form1.terms.checked){
    hideAllErrors();
    document.getElementById("termsError").style.display = "inline";
    document.getElementById("terms").select();
    document.getElementById("terms").focus();
    return false;
  } 

  return true;
}

function check_email(e) {
  ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

  for(i=0; i < e.length ;i++){
    if(ok.indexOf(e.charAt(i))<0){ 
      return (false);
    }   
  } 

  if (document.images) {
    re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
    re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    if (!e.match(re) && e.match(re_two)) {
      return (-1);      
    } 
  }
}
function hideAllErrors() {
  document.getElementById("nameError").style.display = "none"
  document.getElementById("emailError").style.display = "none"
  document.getElementById("commentError").style.display = "none"
  document.getElementById("termsError").style.display = "none"
}

The email and name validation work just fine, the part of the form that won't work looks like this:电子邮件和姓名验证工作正常,表单中不起作用的部分如下所示:

<form onSubmit="return checkForm();" method="get" action="sweepstakes-results.php" 
<input type=checkbox name=terms id=terms ><br></p>
<div class=error id=termsError>Required: Please check the checkbox<br></div>
<p><input type=submit value=Send style="margin-left: 50px"> </p>
</form>

The "terms and conditions" checkbox only works if the file is on my local machine, when I upload it, it just lets me submit the form even if it's not checked. “条款和条件”复选框仅当文件在我的本地机器上时才有效,当我上传它时,它只会让我提交表单,即使它没有被选中。 Isn't JavaScript run on the browser? JavaScript 不是在浏览器上运行的吗? How could the location of the file make a difference?文件的位置有何不同?

Your form doesn't have a name, so the following code won't work:您的表单没有名称,因此以下代码不起作用:

else if (!document.form1.terms.checked){

Since you're already retrieving the DOM object of the checkout, do the following.由于您已经在检索结帐的 DOM 对象,请执行以下操作。 Change the line:更改行:

terms = document.getElementById("terms").value;

to:到:

terms = document.getElementById("terms");

And the replace that else if code with:并将 else if 代码替换为:

else if (!terms.checked){

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

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