Ensuring a Value Is an E-mail Address or URL

Description

The email and url types of the input element ensure that the user has entered a valid e-mail address or fully qualified URL, respectively.

We can combine the pattern attribute with these types to restrict the values, for example, limiting e-mail address to a particular domain.

Example

The following code uses the pattern Attribute with the email input Element Type.


<!DOCTYPE HTML>
<html>
<body>
  <form method="post" action="http://example.com/form">
    <p>
      <label for="email"> Email: <input type="email"
        placeholder="user@mydomain.com" required pattern=".*@mydomain.com$"
        id="email" name="email" />
      </label>
    </p><!--   www . ja  va 2  s.co  m-->
    <input type="submit" value="Submit" />
  </form>
</body>
</html>

Click to view the demo

Note

The code above used three of the validation features.

  • The email type of the input element ensures that use enters a valid e-mail address.
  • The required attribute ensures that the user provides a value.
  • The pattern attribute ensures that an e-mail address belongs to a specific domain (mydomain.com).




















Home »
  HTML CSS »
    HTML »




HTML Introduction
HTML Document
HTML Section
HTML Group Content
HTML Text Marker
HTML Table
HTML Form
HTML Embed