将输入设置为无效

我有两个输入,例如

pass:  pass again:  

我想比较这些输入,如果匹配,则将输入设置为有效。 我尝试过这个,但我认为prop('valid', true); 不起作用:

 $(document).ready(function() { $('input[name=pass2]').keyup(function() { if($('input[name=pass]').val() == $('input[name=pass2]').val()) { $('#pass_hint').empty(); $('#pass_hint').html('match'); $(this).prop('valid', true); } else { $('#pass_hint').empty(); $('#pass_hint').html('mismatch'); $(this).prop('invalid', true); } }); }); 

我创建了一个注册表单,如果密码不一样,输入字段无效,我不能提交这个并给我一些提示。 …而且我不知道如何将此输入设置为无效

在HTMLInputElement接口中 ,没有validinvalid属性。

您可以将setCustomValidity(error)方法与本机表单validation一起使用。

至于你的脚本,这里有一个应该适用于所有HTML5兼容浏览器的演示:

 $('input[name=pass2]').keyup(function () { 'use strict'; if ($('input[name=pass]').val() === $(this).val()) { $('#pass_hint').html('match'); this.setCustomValidity(''); } else { $('#pass_hint').html('mismatch'); this.setCustomValidity('Passwords must match'); } }); 
  

Password:

Verify: