HTML5新特性---Form表單前臺通過正則表達式自動驗證郵箱

在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模式,非常便捷的爲我們提供了郵箱的驗證。


紫皇雷帝

讓學習成爲一種習慣

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章