HTML DOM Input Email pattern Property


The HTML DOM Input Email pattern property sets/returns the regular expression corresponding to Email Input. The pattern attribute’s value is checked against the text typed in an email field.

Syntax

Following is the syntax −

  • Returning regular expression
inputEmailObject.pattern
  • Setting pattern to regular expression
inputEmailObject.pattern = ‘RegExp’

Example

Let us see an example of Input Email pattern property −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<title>Input Email pattern</title>
<style>
   form {
      width:70%;
      margin: 0 auto;
      text-align: center;
   }
   * {
      padding: 2px;
      margin:5px;
   }
   input[type="submit"] {
      border-radius: 10px;
   }
</style>
</head>
<body>
<form onsubmit="checkPattern()">
<fieldset>
<legend>Email-pattern</legend>
<label for="EmailSelect">Employee Email :
<input type="email" id="EmailSelect" pattern="[a-zA-z0-9]+@+MNC.com" title="[a-zA-z0-9]+@+MNC.com">
</label>
<input type="submit" value="Submit">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var inputEmail = document.getElementById("EmailSelect");
   divDisplay.textContent = 'pattern: '+inputEmail.pattern;
   function checkPattern() {
      if(inputEmail.value !== ''){
         var User = inputEmail.value.split("@")[0];
         alert("Welcome "+User);
      }
   }
</script>
</body>
</html>

Output

This will produce the following output −

Before clicking ‘Submit’ button −

After clicking ‘Submit’ button with invalid pattern −

After clicking ‘Submit’ button with valid pattern −

Updated on: 30-Jul-2019

247 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements