I seem to have an issue with HTML5 Form Validation. When I set the custom error message the pattern seems to be incorrect and the field won't validate...
Form
<form action="{PHP_SELF}" method="post" id="register-form" class="register-form">
<div class="">
<label for="username">Username</label>
<input type="text" name="username" id="username"
value=""
pattern="^[a-zA-Z0-9\s]{3,20}$"
required
data-custom-error="Username can only contain characters and numbers and must be between 3 - 20 characters.">
</div>
</form>
JavaScript:
(function() {
let inputs = document.querySelectorAll("#register-form div input");
for (let input in inputs) {
if(inputs.hasOwnProperty(input)) {
let current = inputs[input];
current.setCustomValidity(current.dataset.customError);
}
}
})();