您可以要求两个表单字段与 HTML5 匹配吗? [英] Can you require two form fields to match with HTML5?

查看:18
本文介绍了您可以要求两个表单字段与 HTML5 匹配吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法要求两个表单字段中的条目使用 HTML5 进行匹配?或者这仍然需要用javascript来完成吗?例如,如果您有两个密码字段,并且想要确保用户在每个字段中输入了相同的数据,是否可以通过某些属性或其他编码来实现这一点?

Is there a way to require the entries in two form fields to match using HTML5? Or does this still have to be done with javascript? For example, if you have two password fields and want to make sure that a user has entered the same data in each field, are there some attributes, or other coding that can be done, to achieve this?

推荐答案

不完全是 HTML5 验证,但一点 JavaScript 可以解决问题,请参考以下示例:

Not exactly with HTML5 validation but a little JavaScript can resolve the issue, follow the example below:

<p>Password:</p>
<input name="password" required="required" type="password" id="password" />
<p>Confirm Password:</p>
<input name="password_confirm" required="required" type="password" id="password_confirm" oninput="check(this)" />
<script language='javascript' type='text/javascript'>
    function check(input) {
        if (input.value != document.getElementById('password').value) {
            input.setCustomValidity('Password Must be Matching.');
        } else {
            // input is valid -- reset the error message
            input.setCustomValidity('');
        }
    }
</script>
<br /><br />
<input type="submit" />

这篇关于您可以要求两个表单字段与 HTML5 匹配吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆