H5新增属性

H5新增验证属性

  1.placehplder : 输入框为空时的提示

  2.required : 规定输入框不能为空

  3.pattern : 使用正则表达式规定输入框的数据有效性

JS validity验证属性方法

  1.valueMissing : 输入框不能为空,若为空返回true

  2.typeMismatch : 输入的值是否与输入框类型匹配,若不匹配返回true

  3.patternMismatch : 输入的值是否与规定的正则表达式匹配,若不匹配返回true

自定义提示框内容

  setCustomValidity(值)

代码

html

 1 <form action="" method="post" name="myform">
 2         <dl>
 3             <dt>用户名:</dt>
 4             <dd><input id="user" type="text" pattern="[a-zA-z][a-zA-z0-9]{3,15}" placeholder="以英文字母开头4~16个英文字母或数字"
 5                        required/></dd>
 6         </dl>
 7         <dl>
 8             <dt>密码:</dt>
 9             <dd><input id="pwd" type="password" pattern="[a-zA-z0-9]{4,10}" placeholder="4~10个英文字母或数字" required/></dd>
10         </dl>
11         <dl>
12             <dt>确认密码:</dt>
13             <dd><input id="repwd" type="password"/></dd>
14         </dl>
15         <dl>
16             <dt>电子邮箱:</dt>
17             <dd><input id="email" type="email"/></dd>
18         </dl>
19         <dl>
20             <dt>手机号码:</dt>
21             <dd><input id="mobile" type="text" pattern="1[0-9]{10}"/></dd>
22         </dl>
23         <dl>
24             <dt>生日:</dt>
25             <dd><input id="birth" type="text"
26                        pattern="(19[0-9]{2}|200[0-9]|201[0-6])-(1[0-2]|0[1-9])-([1-2][0-9]|0[1-9]|3[0-1])"/>
27             </dd>
28         </dl>
29         <dl>
30             <dt>&nbsp;</dt>
31             <dd><input name="" type="image" src="images/register.jpg" class="btn"/></dd>
32         </dl>
33     </form>

JavaScript

 1 /**
 2  * Created by Administrator on 2017/7/17.
 3  */
 4 $(function () {
 5     $(".btn").click(function () {
 6         var user = document.getElementById("user");
 7         var pwd = document.getElementById("pwd");
 8         var mobile = document.getElementById("mobile");
 9         var birth = document.getElementById("birth");
10         if(user.validity.valueMissing) user.setCustomValidity("用户名不能为空!");
11             else if(user.validity.patternMismatch) user.setCustomValidity("用户名应以英文字母开头4~16个英文字母或数字!");
12             else user.setCustomValidity("");
13         if(pwd.validity.valueMissing) pwd.setCustomValidity("密码不能为空!");
14             else if(pwd.validity.patternMismatch) pwd.setCustomValidity("密码应为4~10个英文字母或数字!");
15             else pwd.setCustomValidity("");
16         if(mobile.validity.patternMismatch) mobile.setCustomValidity("手机号码为1开头的11位数字!");
17             else mobile.setCustomValidity("");
18         if(birth.validity.patternMismatch) birth.setCustomValidity("生日为1900-2016年格式为1900-01-01或1900-1-1!");
19             else birth.setCustomValidity("");
20     })
21 })

效果

 

 

 

posted @ 2017-07-17 16:46  南橘  阅读(509)  评论(0)    收藏  举报
蜘蛛