Thuộc tính pattern (regex) validate giá trị nhập cho các kiểu: text, url, tel, search, date, email, password. Nếu không khớp, trình duyệt chặn submit và hiển thị thông báo (có thể tùy chỉnh bằng title).
Ví dụ: Mật khẩu tối thiểu 8 ký tự
<!DOCTYPE html>
<html>
<body>
<h2>Đăng ký</h2>
<form action="/new.php">
Tên đăng nhập<br><input type="text" name="name"><br>
Mật khẩu<br><input type="password" name="password" pattern=".{8,}" title="Ít nhất 8 ký tự"><br><br>
<input type="submit" value="Gửi">
</form>
</body>
</html>
Các pattern hữu ích:
- Chỉ chữ cái:
pattern="[A-Za-z]+" - Số điện thoại VN:
pattern="(0[3|5|7|8|9])+([0-9]{8})" - Email domain riêng:
pattern=".+@congty\.com$"