有没有办法要求两个表单字段中的条目使用 HTML 进行匹配?或者这仍然需要用 JavaScript 来完成?例如,如果您有两个密码字段,并且想要确保用户在每个字段中输入了相同的数据,是否可以使用一些属性或其他编码来实现此目的?
不完全是 HTML 验证,但一点 JavaScript 可以解决这个问题,请按照下面的示例操作:
function check() {
var input = document.getElementById('password_confirm');
if (input.value != document.getElementById('password').value) {
input.setCustomValidity('Password Must be Matching.');
} else {
// input is valid -- reset the error message
input.setCustomValidity('');
}
}
<p>
<label for="password">Password:</label>
<input name="password" required="required" type="password" id="password" oninput="check()"/>
</p>
<p>
<label for="password_confirm">Confirm Password:</label>
<input name="password_confirm" required="required" type="password" id="password_confirm" oninput="check()"/>
</p>
<input type="submit" />
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)