欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

css原生输入框验证,:valid和:invalid

程序员文章站 2022-05-31 20:30:42
...

css原生输入框验证,:valid和:invalid

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<style>
	input{
	  display: block;
	  padding: 0 20px;
	  outline: none;
	  border: 1px solid #ccc;
	  width: 150px;
	  height: 40px;
	  transition: all 300ms;
	}
	/*input内容合法时:边框颜色是绿色*/
	input:valid {
	  border-color: green;
	  box-shadow: inset 5px 0 0 green;
	}
	/* input内容非法时:边框颜色是红色*/
	input:invalid {
	  border-color: red;
	  box-shadow: inset 5px 0 0 red;
	}
	button{
	  width: 190px;
	  height: 40px;
	  background-color: green;
	  color: white;
	  margin-top: 20px;
	}
</style>
<body>
	<form>
		<input type="text" maxlength="11" placeholder="请输入手机号码" pattern="^1[3456789]\d{9}$" required>
		<button type="submit">提交</button>
	</form>
</body>
</html>