1 回答
![?](http://img1.sycdn.imooc.com/545845b40001de9902200220-100-100.jpg)
TA贡献1810条经验 获得超4个赞
// Validation function
function validate(element) {
const ErrorStyle = "inset 0px 0px 5px 5px rgba(190, 0, 0, 0.75)";
const ValidStyle = "none";
// Field value is invalid if length is 0 (true = has error)
if (element.value.length === 0) {
element.style.boxShadow = ErrorStyle;
return
}
// Field value is valid if all tests above are false (no errors).
element.style.boxShadow = ValidStyle;
}
// Get input field
const nameField = document.querySelector("#name");
// Bind input event to validation function
nameField.addEventListener('input', function (event) { validate(event.target) });
// Initial validation, comment out below to change to "lazy validation"
validate(nameField);
添加回答
举报