你能否要求两个表单域与HTML5匹配? [英] Can you require two form fields to match with HTML5?

查看:117
本文介绍了你能否要求两个表单域与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天全站免登陆