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>
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).