1 回答

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊
// 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);
添加回答
舉報(bào)