你能否要求两个表单域与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" />