แอตทริบิวต์รูปแบบ HTML กำหนดนิพจน์ทั่วไปซึ่งค่าขององค์ประกอบ HTML จะถูกจับคู่ในเอกสาร HTML
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
<input pattern=”regular expression”>
ให้เรามาดูตัวอย่างรูปแบบ HTML Attribute −
ตัวอย่าง
<!DOCTYPE html>
<html>
<style>
body {
color: #000;
height: 100vh;
background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) no-repeat;
text-align: center;
}
input[type='password'] {
width: 300px;
padding: 8px 16px;
border: 2px solid #fff;
background: transparent;
border-radius: 20px;
display: block;
margin: 1rem auto;
outline: none;
}
.btn {
background: #db133a;
border: none;
height: 2rem;
border-radius: 20px;
width: 330px;
display: block;
color: #fff;
outline: none;
cursor: pointer;
margin: 1rem auto;
}
::placeholder {
color: #000;
}
</style>
<body>
<h1>HTML pattern attribute Example</h1>
<form action="">
<input type="password" placeholder="Enter your account password" required pattern=".{8,}">
<input type="submit" value="Submit" class="btn" onclick='check()'>
</form>
<div class="show"></div>
<script>
function check() {
if (document.querySelector("input[type='password']").value.length < 8) {
document.querySelector(".show").innerHTML = 'Please enter a password of more than or equal to 8 characters.'
};
}
</script>
</body>
</html> ผลลัพธ์

ป้อนรหัสผ่านที่มีความยาวน้อยกว่า 8 ตัวอักษร จากนั้นคลิกที่ “ส่ง ” เพื่อแสดงข้อความเตือน
