简体   繁体   English

无论验证如何,表格都会提交

[英]Form submits regardless of validation

I have a form, and I've written a validation script for it, however, it's not working 100%. 我有一个表单,我已经为它写了一个验证脚本,然而,它没有100%工作。 It outputs errors fine, but the submit button will submit the form, even if it has outputted the alert boxes. 它输出错误很好,但提交按钮将提交表单,即使它已输出警告框。 Anyone have any idea why? 任何人都知道为什么?

Apparently not all the code pasted. 显然不是所有代码都粘贴了。 I would just use the Required parameter, but I need JS validation as it is an assignment. 我只想使用Required参数,但我需要JS验证,因为它是一个赋值。 Also, UL is defined before this part of code, as there is a list before this. 此外,UL在此部分代码之前定义,因为在此之前有一个列表。

HTML: HTML:

<div class = "form">
            <form name = "contactForm" onsubmit="validateForm()" action = "form.php">

            <li><label>First name: </label><input type = "text" name = "fname" autofocus></li><br>

            <li><label>Last Name: </label><input type = "text" name = "lname"></li><br>

            <li><label>Email: </label><input type = "text" name = "email"> <button onclick = "validateEmail();return false">Check if email is valid</button> </li><br>

            <li><label>Message: </label> <br>

                <textarea rows = "10" cols = "50" name = "message"></textarea></li>

            <li> <input type = "submit"> </li>

            </form>

JavaScript: JavaScript的:

function validateForm()
{
  var x=document.forms["contactForm"]["fname"].value; //Gets the form and field name from the HTML

  if (x==null || x=="") //If the field "fname" contains null, or nothing, then output an alert telling the user to input something into the field. Same goes for the rest of the code.
  {
    alert("First name must be filled out");
    return false;
  }

  var x=document.forms["contactForm"]["lname"].value;

  if (x==null || x=="") 
  {
    alert("Last name must be filled out");
    return false; 
  }

  var x=document.forms["contactForm"]["email"].value;

  var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; 

  if(reg.test(x) == false)
  {
      alert("Please enter a valid Email");
      return false;
  }

  if (x==null || x=="")
  {
    alert("Email must be filled out");
    return false;
  }

  var x=document.forms["contactForm"]["message"].value;

  if (x==null || x=="")
  {
    alert("Message must be filled out");
    return false;
  }
}

You have to trigger event on time of submit. 您必须在提交时触发事件。 Like this way: 像这样:

<form onsubmit="validateForm()">

How are you attaching the event listener? 你是如何附加事件监听器的? I'd wager it's with: 我打赌它是:

<form … onsubmit="validateForm()">

You'd need return validateForm() . 你需要return validateForm() But wait! 可是等等! Don't add that. 不要添加。

Your script does not check for valid e-mail addresses correctly. 您的脚本不会正确检查有效的电子邮件地址。 It only checks one error at once. 它只能一次检查一个错误。 It will annoy users. 它会惹恼用户。 Use HTML5 validation and back it up with PHP. 使用HTML5验证并使用PHP进行备份。

  • By applying type="email" , you don't need a button at all, and mobile users will see an e-mail specific keyboard if available. 通过应用type="email" ,您根本不需要按钮,移动用户将看到特定于电子邮件的键盘(如果可用)。 The browser will validate it, given support. 在给定支持的情况下,浏览器将验证它。

     <input type=" email " name="email"> 
  • Required fields should use the required attribute. 必填字段应使用required属性。

     <input type="text" name="fname" autofocus required > ⋮ <input type="text" name="lname" required > 

Your labels aren't correct either; 你的标签也不正确; they should surround the element they're related to, or provide a for attribute matching the element's id . 他们应该围绕他们相关的元素,或者提供一个for属性匹配元素的id

You should also validate your HTML. 您还应该验证您的HTML。 And not put <br> s between <li> s. 而不是把<br>之间采用S <li>秒。

Finally, as general [client-side] JavaScript tips: 最后,作为一般[客户端] JavaScript提示:

  • You don't have to check the value of a text field for null . 您不必检查文本字段的value是否为null
  • Write !x instead of x == false . !x而不是x == false

You can also use JQuery to do something like this: 你也可以使用JQuery做这样的事情:

    $( "#formId" ).submit(function( event ) {
  if($("#nameInput").val() == "")
      event.preventDefault();
});

So basically, if nameInput's equals to empty the submit action will be canceled 所以基本上,如果nameInput等于空,则提交操作将被取消

Take a look at here if you want: https://api.jquery.com/submit/ 如果您需要,请看这里: https//api.jquery.com/submit/

Good luck 祝好运

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

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