Computer >> Computer tutorials >  >> Programming >> HTML

How to add a regular expression that an <input> element's value is checked against in HTML?


Use the pattern attribute to set the regular expression that an <input> element’s value is checked. You can try to run the following code to implement pattern attribute −

Example

<!DOCTYPE html>
<html>
   <head>
      <title>HTML novalidate attribute</title>
   </head>
   <body>
      <form action = "" method = "get" novalidate>
         Student Name<br><input type = "name" name = "sname"><br>
         Rank<br><input type = "number" name = "rank"><br>
         Student Country <br><input type = "text" name = "ccode" pattern="[A-Za-z]{3}" title = "country code"><br>

         <input type = "submit" value = "Submit">
      </form>
   </body>
</html>