Can you require two form fields to match with HTML?

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

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" />

Leave a Comment