简体   繁体   中英

HTML5 validation and field comparison

Is there currently any way to make a comparison check between 2 fields to check either that they match or do not match in HTML5 validation? Or would you have to write your own Javascript to do it?

使用HTML5无法完成那种检查。

Not exactly with HTML5 validation but a little JavaScript can resolve the issue, following is a general example regarding your question:

<form method="post" enctype="multipart/form-data" action="Your_Action_Page.php">
<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" />
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM