ปัญหา
การทำเครื่องหมายช่องที่ต้องกรอกด้วยสถานะข้อผิดพลาดทันทีที่โหลดหน้าเว็บอาจทำให้ผู้ใช้สับสน ตามหลักการแล้ว ช่องที่ต้องกรอกควรแสดงเป็น "ไม่ถูกต้อง" เฉพาะในกรณีที่ผู้ใช้พยายามกรอกข้อมูลแล้วแต่ไม่สำเร็จ
วิธีแก้ปัญหา
คลาสเทียม :user-invalid ช่วยแก้ปัญหานี้ได้อย่างสมบูรณ์แบบ สำหรับช่องที่ต้องกรอก ระบบจะไม่จับคู่เมื่อการโหลดหน้าเว็บ โดยจะจับคู่ก็ต่อเมื่อเป็นไปตามเงื่อนไขต่อไปนี้เท่านั้น
- ผู้ใช้โต้ตอบกับช่อง (เช่น พิมพ์อักขระและลบออก) แล้วออกจากช่อง (เบลอ) โดยปล่อยให้ช่องว่างเปล่า
- ผู้ใช้พยายามส่งแบบฟอร์มขณะที่ช่องว่างเปล่า
กลยุทธ์การติดตั้งใช้งาน
- ข้อจำกัด HTML: เพิ่มแอตทริบิวต์
requiredลงในอินพุต - การตอบกลับด้วยภาพ: ใช้
:user-invalidเพื่อจัดรูปแบบเส้นขอบสีแดงและแสดงข้อความช่วยเหลือ "จำเป็น" - เวลา: อาศัยเวลาตามปกติของเบราว์เซอร์สำหรับการแสดงผลภาพ คุณไม่จำเป็นต้องใช้แฮนเดิล
onBlurเพื่อเพิ่มคลาสtouchedอีกต่อไป แม้ว่าจะยังคงต้องใช้ JavaScript บางส่วนเพื่อซิงค์แอตทริบิวต์ ARIA (ดูด้านล่าง)
คู่มือการติดตั้งใช้งาน
1. โครงสร้าง HTML
<form id="feedback-form">
<div class="field">
<label for="full-name">Full Name</label>
<input
type="text"
id="full-name"
name="full-name"
required
aria-errormessage="name-error"
>
<!-- MANDATORY: Include an icon or distinct non-color indicator alongside error text -->
<div id="name-error" class="error-msg">
<span aria-hidden="true">❌</span> This field is required.
</div>
</div>
</form>
2. CSS
.error-msg {
display: none;
color: #d93025;
font-size: 0.875rem;
margin-top: 0.25rem;
}
/*
Only highlight empty required fields AFTER the user visits them.
MANDATORY: Provide multiple indicators (border shift + helper text/icon) to avoid color-only state communication.
*/
input:user-invalid {
border-color: #d93025;
background-color: #fce8e6;
}
input:user-invalid + .error-msg {
display: block;
}
/* Optional: Subtle indicator for required fields that are valid */
input:required:user-valid {
border-color: #188038;
border-width: 2px;
}
3. การซิงค์สถานะ JavaScript
บังคับ: เนื่องจาก :user-invalid เป็นสถานะภาพ คุณจึงต้องจัดเตรียมบริดจ์ JavaScript เพื่อซิงค์ aria-invalid="true" แบบไดนามิกสำหรับเทคโนโลยีความช่วยเหลือพิเศษเมื่อผู้ใช้เบลอช่องที่ไม่ถูกต้องหรือพยายามส่งข้อมูล
const form = document.getElementById('feedback-form');
const syncAriaInvalid = (input) => {
if (!input.checkValidity()) {
input.setAttribute('aria-invalid', 'true');
} else {
input.removeAttribute('aria-invalid');
}
};
// Sync on blur when a user finishes interacting
form.addEventListener('blur', (e) => {
if (e.target.matches('input[required]')) {
syncAriaInvalid(e.target);
}
}, true);
// Sync all required fields when submission is attempted
form.addEventListener('submit', () => {
form.querySelectorAll('input[required]').forEach(syncAriaInvalid);
});
// Remove error state immediately upon correction
form.addEventListener('input', (e) => {
if (e.target.matches('input[required]') && e.target.checkValidity()) {
e.target.removeAttribute('aria-invalid');
}
});
การสำรองและการรองรับเบราว์เซอร์
การสนับสนุนเบราว์เซอร์และทางเลือกสำรองสำหรับ :user-valid และ :user-invalid
CSS สำหรับโฆษณาสำรอง
input:user-invalid,
input.user-invalid-fallback {
border-color: #d93025;
background-color: #fce8e6;
}
input:user-invalid + .error-msg,
input.user-invalid-fallback + .error-msg {
display: block;
}
JavaScript สำรอง
ใช้ยูทิลิตีที่นำกลับมาใช้ใหม่ได้ซึ่งติดตามสถานะการโต้ตอบโดยใช้ WeakMap วิธีนี้จะช่วยหลีกเลี่ยงการทำให้ DOM มีคลาสหรือแอตทริบิวต์ข้อมูลที่ "ไม่ถูกต้อง"
const UserInvalidFallback = (() => {
const dirtyState = new WeakMap();
const updateState = (input) => {
const isValid = input.checkValidity();
// Update both visual and ARIA state
input.classList.toggle('user-invalid-fallback', !isValid);
input.classList.toggle('user-valid-fallback', isValid);
if (!isValid) {
input.setAttribute('aria-invalid', 'true');
} else {
input.removeAttribute('aria-invalid');
}
};
const handleEvent = (event) => {
const input = event.target;
if (event.type === 'reset') {
const controls = input.elements || [];
for (const control of controls) {
dirtyState.delete(control);
control.classList.remove('user-invalid-fallback');
control.classList.remove('user-valid-fallback');
control.removeAttribute('aria-invalid');
}
return;
}
if (!input.checkValidity) return;
if (event.type === 'input' || event.type === 'change') {
const state = dirtyState.get(input) || { hasInteracted: false, hasBlurred: false };
state.hasInteracted = true;
dirtyState.set(input, state);
if (state.hasBlurred) {
updateState(input);
}
} else if (event.type === 'blur') {
const state = dirtyState.get(input) || { hasInteracted: false, hasBlurred: false };
state.hasBlurred = true;
dirtyState.set(input, state);
if (state.hasInteracted) {
updateState(input);
}
}
};
const init = (root = document) => {
if (CSS.supports('selector(:user-invalid)')) return;
root.addEventListener('blur', handleEvent, true); // Capture phase
root.addEventListener('input', handleEvent);
root.addEventListener('change', handleEvent);
root.addEventListener('reset', handleEvent, true); // Capture resets
};
return { init };
})();
// Initialize for a specific form
const form = document.querySelector('#demo-form');
UserInvalidFallback.init(form);
ปัจจัยอื่นๆ ที่ควรพิจารณา
- เครื่องหมายดอกจัน: แนวทางปฏิบัติแนะนำยังคงเป็นการระบุช่องที่ต้องกรอกด้วยภาพ (เช่น มีเครื่องหมายดอกจัน
*) ในป้ายกำกับ เพื่อให้ผู้ใช้ทราบว่าต้องทำอะไรก่อนที่จะโต้ตอบ - ปุ่มส่ง: เปิดใช้ปุ่มส่งไว้เสมอ ซึ่งต่างจากปุ่ม
disabledหากผู้ใช้คลิก เบราว์เซอร์จะทริกเกอร์:user-invalidในช่องที่ต้องกรอกซึ่งว่างอยู่ทั้งหมดโดยอัตโนมัติ และโฟกัสที่ช่องแรก ซึ่งเป็นประโยชน์อย่างยิ่งต่อการช่วยเหลือพิเศษและ UX - การช่วยเหลือพิเศษ:
:user-invalidแบบเนทีฟจะไม่ซิงค์กับแอตทริบิวต์ ARIA โดยอัตโนมัติ เพิ่ม JavaScript ต่อไปนี้เพื่อให้aria-invalidซิงค์กับสถานะภาพ
// Sync aria-invalid with the CSS :user-invalid state
const syncAria = (el) => {
el.setAttribute?.('aria-invalid', el.matches(':user-invalid') ? 'true' : 'false');
};
// Update on blur (to show error) and input (to clear it)
document.addEventListener('blur', (e) => syncAria(e.target), true);
document.addEventListener('input', (e) => {
if (e.target.hasAttribute('aria-invalid')) syncAria(e.target);
});