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
  <label for="password">Password:</label>
  <input name="password" required="required" type="password" id="password" oninput="check()"/>

  <label for="password_confirm">Confirm Password:</label>
  <input name="password_confirm" required="required" type="password" id="password_confirm" oninput="check()"/>

<input type="submit" />

Leave a Comment