<!DOCTYPE html>
<html>
<head>
  <mata charset="gb2312">
  <title>form test</title>
</head>
 
<body>
  <form name="test" >
    <input type="text" required pattern="^\d{4}$" oninput="out(this,'输入四位数数字')" placeholder="请输入代码" >
	
	<select required>
	  <option>请选择</option>
	  <option>111</option>
	  <option>111</option>
	  <option>111</option>
	</select>
    <button type="submit">Check</button>
  </form>
<script type="text/javascript">

function out(i,tip){
var v = i.validity;//
if(true === v.patternMismatch){//规则验证
i.setCustomValidity(tip);
}else{
i.setCustomValidity("");
}

}

</script>
</body>
</html>

  2http://www.jb51.net/article/64749.htm