The pattern attribute is used to define a regular expression that the input value must match. It helps validate user input before form submission.
- Specifies a pattern or format for the input value.
- Commonly used with text, password, email, and telephone input fields.
- If the entered value does not match the pattern, the form cannot be submitted.
Syntax:
<input pattern = "regular_exp">Example: Illustrates the use of pattern attribute in <input> element.
<!--Driver Code Starts-->
<!DOCTYPE html>
<html>
<head>
<title>
HTML input pattern attribute
</title>
<style>
body {
text-align:center;
}
h1 {
color:green;
}
</style>
</head>
<!--Driver Code Ends-->
<body>
<h1>GeeksforGeeks</h1>
<h2>HTML <input>pattern attribute</h2>
<form action="#">
Password: <input type="text" name="Password"
pattern="[A-Za-z]{3}" title="Three letter Password">
<input type="submit">
</form>
</body>
<!--Driver Code Starts-->
</html>
<!--Driver Code Ends-->