简体   繁体   English

如何创建 html5 自定义验证?

[英]How to create html5 custom validation?

I am using html 5 form validation for validate my form before submit, if is valid, submit, but I need validate my User Register form, so it need validate if Password Confirm value is equal camp Password, below is my form example:我在提交之前使用 html 5 表单验证来验证我的表单,如果有效,则提交,但我需要验证我的用户注册表单,所以它需要验证密码确认值是否等于 camp 密码,下面是我的表单示例:

<form>
    <label>Login:</label>
    <input type="text" name="login" id="login"/><br/>
    <label>Password:</label>
    <input type="password" name="pass" id="pass"/><br/>
    <label>Password Confirm:</label>
    <input type="password" name="pass_conf" id="pass_conf"/><br/>

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

or in jsfiddle或者在 jsfiddle 中

How to can I create my custom validation for work like default validations?如何为默认验证之类的工作创建自定义验证?

Well you can use JQuery and attach an attribute to be selected for the passwords to validate each other via input event. 好吧,您可以使用JQuery并附加一个要为密码选择的属性,以通过input事件相互验证。 Use setCustomValidity() to set the message of the input affected to override the default message when the form is submitted. 使用setCustomValidity()设置受影响的输入消息,以在提交表单时覆盖默认消息。

See the updated fiddle . 请参阅更新的小提琴

As you can see in the fiddle, all you have to do is add an attribute data-equal-id wherein the attribute value must be the ID of password input element to be tested. 正如您在小提琴中看到的,您要做的就是添加一个属性data-equal-id其中属性值必须是要测试的密码输入元素的ID。

HTML 的HTML

<h1>How to create html5 validation for password confirm?</h1>
<hr>
<form>
    <label>Login:</label>
    <input type="text" name="login" id="login"/><br/>
    <label>Password:</label>
    <input type="password" name="pass" id="pass"/><br/>
    <label>Password Confirm:</label>
    <input type="password" name="pass_conf" id="pass_conf" data-equal-id="pass" /><br/>
    <input type="submit"/>
</form>

Javascript Java脚本

$('[data-equal-id]').bind('input', function() {
    var to_confirm = $(this);
    var to_equal = $('#' + to_confirm.data('equalId'));

    if(to_confirm.val() != to_equal.val())
        this.setCustomValidity('Password must be equal');
    else
        this.setCustomValidity('');
});

you could try putting this code in your header: 您可以尝试将此代码放在标头中:

<script>
document.getElementById('myform').onsubmit = function() {

    if(!validateForm()){ // call your validation function
        alert('fail!'); // remove this
        return false; // prevent the form to submit
    }
}

// your validation function
// compares that the passwords are equal
function validateForm(){
    var pass = document.getElementById('pass').value;
    var pass_conf = document.getElementById('pass_conf').value;

    if(pass == pass_conf){
        return true;
    }else{
        return false;
    }

}
</script>

also put the id 'myform' to your form (or the name you want, but change it in the first line) 还将ID“ myform”放入您的表单(或您想要的名称,但在第一行中进行更改)

How about something fun like this using jQuery? 使用jQuery这样有趣的事情怎么样?

$('input[type="password"]').keyup(function() {
  var pass=$('#pass').val();
  var conf=$('#pass_conf').val();
  if (pass == conf)
    $('input[type="submit"]').removeAttr('disabled');
  else
    $('input[type="submit"]').attr('disabled', 'disabled');
});

The breakdown... 故障...

  • I am keying off of the keyup, so every time a key is pressed in the password fields the function will fire. 我正在按键盘上的键,因此每次在密码字段中按一个键时,该功能都会触发。
  • I'm grabbing the value of both password fields, and comparing them. 我正在获取两个密码字段的值,并进行比较。
  • If the values are the same, I'm enabling the submit button. 如果值相同,则启用提交按钮。
  • If the values are different, I'm disabling the submit button. 如果值不同,则禁用提交按钮。

Pretty simple, but it works. 很简单,但是可以。 Here is a demo: http://codepen.io/anon/pen/GxAyC/ 这是一个演示: http : //codepen.io/anon/pen/GxAyC/

(note - I added a couple of other visual enhancements to the demo to show what can be done) (注意-我在演示中添加了其他一些视觉增强功能,以显示可以完成的操作)

You're using HTML5 for client-side form validation and wish to validate your form prior to form submission.您正在使用 HTML5 进行客户端表单验证,并希望在提交表单之前验证您的表单。 Your form consists of three inputs and your only validation criteria is that both password fields match.您的表单包含三个输入,您唯一的验证标准是两个密码字段都匹配。

The most simple way to do this is to write a custom submit handler script:最简单的方法是编写自定义提交处理程序脚本:

const handleFormSubmit = (event) => {
  event.preventDefault();
  form = event.target;
  if (form.pass === form.pass_conf) {
     form.submit();
  }
}

Above preventDefault() stops the default form submission behavior so you can execute your check.上面的preventDefault()停止了默认的表单提交行为,因此您可以执行检查。 Then check if the value of the two password fields are equal.然后检查两个密码字段的值是否相等。 And if they are, continue form submission.如果是,请继续提交表单。

To use, attach the custom handler to your form by listening to the submit event:要使用,请通过监听submit事件将自定义处理程序附加到您的表单:

const form = document.querySelector('form');
form.addEventListener('submit', handleFormSubmit);

Applied in context to example form provided:在上下文中应用于提供的示例表单:

<form>
  <label>Login:</label>
  <input type="text" name="login" id="login"/><br/>
  <label>Password:</label>
  <input type="password" name="pass" id="pass"/><br/>
  <label>Password Confirm:</label>
  <input type="password" name="pass_conf" id="pass_conf"/><br/>

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

<script>
  const form = document.querySelector('form');
  form.addEventListener('submit', handleFormSubmit);

  const handleFormSubmit = (event) => {
    event.preventDefault();
    form = event.target;
    if (form.pass.value === form.pass_conf.value) {
       form.submit();
    }
  }
</script>

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

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