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