<form>
<p><label>パスワード <input type=password name=password required pattern="[!-~]{5,}" title="英数字5文字以上"></label>
<p><label>もういちど <input type=password name=password2 required pattern="[!-~]{5,}" title="英数字5文字以上"
oninput=" this.setCustomValidity(this.form.elements.password.value === this.value ? '' : this.getAttribute('data-password-mismatch-error')) "
data-password-mismatch-error="同じパスワードを記入してください。"></label>
<p><button type=submit>設定</button>
</form>
setCustomValidity で独自のバリデーションエラーメッセージを設定することができます。エラー状態ではフォームを提出できず、ユーザーにエラーメッセージが表示されます。空文字列を指定するとエラー状態を解除できます。