Note: to the best of my knowledge this question is not a duplicate question of the following:
Given a field that:
pattern
attribute set for validation, for example "[a-f,0-9]{4}"
for a 4 character hex string input.oninvalid
set with setCustomValidity('...some message...')
to define a custom validation messageoninput
set with setCustomValidity('')
to reset on inputHere is an example showing this:
/* jshint esnext: true */
const form = document.querySelector("#form");
const field = document.querySelector("#field");
const output = document.querySelector("#output");
form.addEventListener('submit', (e) => {
console.log("SUBMIT");
output.textContent = field.value;
e.preventDefault(); // Prevent default POST request
});
field.oninvalid = (event) => {
console.log("INVALID");
event.target.setCustomValidity('must be valid 4 hex characters');
}
field.oninput = (event) => {
console.log("INPUT");
event.target.setCustomValidity('');
}
Output: <span id="output">No output</span>
<form id="form">
<label for="field">Enter 4 character hex code: </label>
<input id="field" type="text" pattern="[a-f,0-9]{4}" autocomplete=off>
</form>
Validation works almost as desired, except when the user enters an invalid entry and then proceeds to try and edit it, where their following input states are still invalid:
At this point, neither the custom setCustomValidity
message defined in oninvalid
is used, nor the empty one defined in onInput
.
Instead, as long as the field is in an invalid state and not blurred, the default Please match the requested format.
message appears.
What is going on here? Looking at the console, the oninput
event is called each time, and therefore event.target.setCustomValidity('');
is called each time.
So why is it that we are still seeing the generic default validation message? Shouldn't setCustomValidity('')
disable that?
An acceptable answer here should exhibit the following:
parameter
field is respected for validation.Please match the requested format.
message never appears at all.It appears that this is a bug with Chrome 65 in windows.
using setCustomValidity('')
in oninput
should disable the default validation messages appearing on input.
The following workaround works for me:
/* jshint esnext: true */
const form = document.querySelector("#form");
const field = document.querySelector("#field");
const output = document.querySelector("#output");
const pattern = field.getAttribute("pattern");
form.addEventListener('submit', (e) => {
console.log("SUBMIT");
output.textContent = `User submitted: ${field.value}`;
e.preventDefault(); // Prevent default POST request
});
field.oninvalid = (event) => {
console.log("INVALID");
event.target.setCustomValidity('must be valid 4 hex characters');
}
field.oninput = (event) => {
console.log("INPUT");
event.target.setCustomValidity('');
event.target.removeAttribute("pattern");
}
field.onchange = (event) => {
console.log("CHANGE");
event.target.setAttribute("pattern", pattern);
}
Output: <span id="output">No output</span>
<form id="form">
<label for="field">Enter 4 character hex code: </label>
<input id="field" type="text" pattern="[a-f,0-9]{4}" autocomplete=off>
</form>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With