HTML <input> pattern Attribute

Last Updated : 26 May, 2026

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. 

html
<!--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 &lt;input&gt;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-->
Comment