在HTML5中Form表单中添加了许多新特性,下面为大家介绍一种form表单中的新属性pattern。
pattern 属性规定用于验证 input 域的模式(pattern),模式(pattern) 是正则表达式。
注释:pattern 属性适用于以下类型的 <input> 标签:text, search, url, telephone, email 以及 password。
下面给大家一个我写的一个input="text" 文本输入框自动验证邮箱的例子。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>正则匹配邮箱</title>
</head>
<body>
<form action="">
邮箱:<input type="text" pattern="^[a-zA-Z][0-9a-zA-Z]*@{1}[0-9a-zA-Z]+\.(com|net|cn|com\.cn)$">
<br><input type="submit" value="提交">
</form>
</body>
</html>
大家可以运行查看一下效果,只有在输入正确的邮箱格式时,才可以提交Form表单。
pattern模式,非常便捷的为我们提供了邮箱的验证。
紫皇雷帝
让学习成为一种习惯
转载于:https://blog.51cto.com/leidi/1607619