javascript - Javascript form validation: custom error message
问题描述
https://codepen.io/durja/pen/BPmmyR
I am trying to implement custom error messages for a form using the constraint api in html. Below is my code and I am facing an issue where after entering a wrong input and correcting it again, the validation message is still showing up. I am not sure what I am missing. I even tried to check the validity using checkValidity() method which is returning false even though the input is of the correct pattern.
const fp = document.getElementById('quantity');
const ide = document.getElementById('ide_pref');
fp.addEventListener('input',e => {
console.log(e.target.value);
if(fp.validity.rangeOverflow) {
fp.setCustomValidity('Custom message: greater than 100 not allowed.');
}
});
ide.addEventListener('input',e => {
console.log(e.target.value);
console.log(ide.checkValidity());
if(ide.validity.patternMismatch) {
ide.setCustomValidity('enter exactly webstorm OR vscode');
}
})
解决方案
See HTMLSelectElement.setCustomValidity()
The HTMLSelectElement.setCustomValidity() method sets the custom validity message for the selection element to the specified message. Use the empty string to indicate that the element does not have a custom validity error.
You're currently never resetting the custom validity message, so it remains 'Custom message: greater than 100 not allowed.'
(or the other message for the other element) forever whenever an input is invalid. Insert else
statements (if there is no rangeOverflow
or no patternMismatch
) that call setCustomValidity
with the empty string to clear the error messages, in case any exist when the button is clicked:
const fp = document.getElementById('quantity');
const ide = document.getElementById('ide_pref');
fp.addEventListener('input', e => {
if (fp.validity.rangeOverflow) {
fp.setCustomValidity('Custom message: greater than 100 not allowed.');
} else {
fp.setCustomValidity('');
}
});
ide.addEventListener('input', e => {
if (ide.validity.patternMismatch) {
ide.setCustomValidity('enter exactly webstorm OR vscode');
} else {
ide.setCustomValidity('');
}
})
.u {
margin-bottom: 15px;
}
.u:invalid {
border: 2px solid orangered;
}
.u:valid {
border: 2px solid greenyellow;
}
.btn {
border: none;
padding: 10px;
border-radius: 8px;
margin-top: 10px;
}
:focus {
outline-color: gray;
}
<form action="#" method="get">
<div>
<label for="ide_pref">Would you prefer webstorm or vscode?</label>
<input required class="u" type="text" pattern="webstorm|vscode" id="ide_pref" name="ide_pref"><br>
<label for="quantity">How many licences would you like?</label>
<input required class="u" type="number" min="1" max="100" id="quantity" name="lic_quantity"><br>
<button class="btn" type="submit">Submit</button>
</div>
</form>
推荐阅读
- perl - 在 shebang 行中使用“-w”
- postgresql - 如何在 jsonb 字段中查找数组元素?
- ios - 使用 Flutter 插入 sqflite 时出现问题
- cmd - 如何通过 cmd 命令和注册表查看所有登录到我的计算机的用户(以及最近登录的时间?)
- laravel - 如何在同一 IP 的 nginx 中一次增加 5 个以上的请求
- python - '保存'和'另存为'使用 QFileDialog.getSaveFile()
- javascript - WebDriverIO 6 – $ 工作正常,$ 在 iframe 中不起作用
- delta-lake - Delta Lake Table 元数据
- vue.js - Vue中从子组件导航到父组件
- selenium - 无法在 aws 设备场中截取屏幕截图以进行 android 应用程序自动化测试