HTML元素:
<input type="text" name="title" id="title" value="${myContent.title}"
οnkeyup="validateNotEmptyField(document.getElementById('title'))"
οnsubmit="validateNotEmptyField(document.getElementById('title'))"
οnchange="validateNotEmptyField(document.getElementById('title'))">
JavaScript代码:
function validateNotEmptyField(field) {
if (field.value == '' || field.value == null) {
field.setCustomValidity('不能为空, 请输入');
} else {
field.setCustomValidity('');
}
}