How to use pattern attribute in HTML?

HTMLFront End TechnologyWeb Development

The pattern attribute in HTML is used to set a regular expression in the following input types: text, url, tel, search, date, email, and password. For passwords, with the pattern attribute, you can set the minimum required values. For example, password should have at least 8 characters.

Example

You can try to run the following code to learn how to use pattern attribute in HTML. Here, we have set the pattern for 8 or more characters in the password. If a user will add less than that, then by clicking Submit button, an error will raise i.e. “Please match the requested format: Eight or more characters”.

<!DOCTYPE html>
<html>
   <body>
      <h2>Register</h2>
      <form action="/new.php">
         Student UserName<br><input type = "text" name = "name"/><br>
         Password<br><input type = "password" name = "password" pattern = ".{8,}"
         title="Eight or more characters"><br><br>
         <input type = "submit" value = "Submit">
      </form>
   </body>
</html>
raja
Published on 24-Jan-2018 07:44:05
Advertisements