简体   繁体   English

使用JavaScript Submit()函数时如何运行HTML5表单验证?

[英]How to run HTML5 form verification when using JavaScript submit() function?

When having an input such as this: 当有这样的输入时:

<form action="go" method="post">
    <input name='submitthingy' required>
</form>

It should only be able to submit when the field is filled in. This works when I use a submit button like this: 它仅应在填写字段时才能提交。当我使用如下所示的提交按钮时,此方法有效:

<button type='submit'>Click Me</button>

It makes a small popup appear, asking to fillin the field. 它会出现一个小的弹出窗口,要求填写该字段。 However, when I submit it with javascript, like this: 但是,当我使用javascript提交时,如下所示:

document.querySelector('form').submit();

It submits the from but does not verify that the field is filled in first. 它提交来自,但不验证是否首先填写了该字段。 How should I do this? 我应该怎么做?

尝试调用“提交”按钮的点击操作,而不是调用表单的“提交”操作:

document.getElementsByTagName("button")[0].click();

You can give the button an id and call document.getElementById("btnsubmit").click() 您可以给按钮一个ID并调用document.getElementById(“ btnsubmit”)。click()

If you would prefer not to display the button add a display none. 如果您不想显示该按钮,则不显示任何内容。

<button id='btnsubmit' type='submit' style='display:none;'>Click Me</button>

Illustrating the API from my comment: 从我的评论中说明API:

 var myform = document.getElementById('myform'); var fields = myform.querySelectorAll('input,textarea,select'); var test = document.getElementById('test'); test.addEventListener('click', function(evt) { var valid = Array.prototype.every.call(fields, function(field) { return field.checkValidity(); }); test.style.backgroundColor = valid ? "green" : "red"; }); 
 <form id="myform"> <p> <label for="dog">Dog:</label> <input pattern="Dog" id="dog" name="dog"> </p> </form> <button id="test">Test</button> 

The JavaScript way to handle this would be to use the constraint validation API to check each field as the user interacts with it, then again on form submit. 处理此问题的JavaScript方法是使用约束验证API在用户与其交互时检查每个字段,然后再次进行表单提交。

Use a keyUp event listener to watch each field for changes to check for their validity, then a submit event listener to check the value of each field again before sending. 使用keyUp事件侦听器来观看每个字段更改,以检查其有效性,然后submit事件监听器在发送前再次检查每个字段的值。 Below I've created an example shamelessly lifted and only lightly edited from the linked MDN page. 下面,我从链接的MDN页面创建了一个毫不客气地进行修改的示例。

Your form might look something like this: 您的表单可能看起来像这样:

<!-- form disables HTML validation -->
<form novalidate>
    <label for='submitthingy'>
        <!-- input field and error span are in the same label -->
        <input id='submittable' name='submitthingy' required>
        <!-- error to be called by JavaScript -->
        <span class='error' aria-live='polite'></span>
    </label>
<button type='submit'>Click Me</button>
</form>

And your JS like this: 和你的JS是这样的:

var form  = document.getElementsByTagName('form')[0];
var submitthingy = document.getElementById('submittable');
var error = document.querySelector('.error');

// One of these for each field, or you could DRY it up somehow
email.addEventListener("keyup", function (event) {
  if (submitthingy.validity.valid) {
    error.innerHTML = "";
    error.className = "error";
  }
}, false);
form.addEventListener("submit", function (event) {
  if (!submitthingy.validity.valid) {
    error.innerHTML = "This field is not valid per CSS pseudo class!";
    error.className = "error active";

event.preventDefault();
  }
}, false);

Then you can style your error messages using CSS to hide them by default, then apply styles based on the class(es) error active . 然后,您可以使用CSS为错误消息设置样式,以默认情况下将其隐藏,然后根据error active类别error active应用样式。

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

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