HTMLTextAreaElement: setCustomValidity() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Phương thức setCustomValidity() của giao diện HTMLTextAreaElement đặt thông báo hợp lệ tùy chỉnh cho phần tử <textarea>. Dùng chuỗi rỗng để cho biết phần tử không có lỗi hợp lệ tùy chỉnh.

Cú pháp

js
setCustomValidity(string)

Tham số

string

Chuỗi chứa thông báo lỗi. Chuỗi rỗng sẽ loại bỏ mọi lỗi hợp lệ tùy chỉnh.

Giá trị trả về

Không có (undefined).

Ví dụ

Trong ví dụ này, nếu <textarea> không vượt qua xác thực ràng buộc, chúng ta cung cấp lỗi tùy chỉnh dựa trên ràng buộc nào đang thất bại. Nếu giá trị hợp lệ, chúng ta đặt lỗi tùy chỉnh thành chuỗi rỗng:

js
const comment = document.getElementById("comment");
if (comment.validity.valueMissing) {
  comment.setCustomValidity("We can't submit a blank comment!");
} else if (comment.validity.tooShort) {
  comment.setCustomValidity("Tell us more! Your comment is too short.");
} else if (comment.validity.tooLong) {
  comment.setCustomValidity(
    "Loquacious much? Keep it to under 800 characters!",
  );
} else {
  comment.setCustomValidity("");
}

Đặc tả kỹ thuật

Thông số kỹ thuật
HTML
# dom-cva-setcustomvalidity-dev

Tương thích trình duyệt

Xem thêm