html input输入验证不为空

简介:

html5的话使用required即可,比较简单。但是ie10以下的浏览器没有required特性。所以就要靠其他方式来对用户输入做验证。下面是利用js的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
< script  type = "text/JavaScript" >
function validate(){
    var domain = document.getElementById('domain').value;
    var domainInfo = document.getElementById('domainInfo');
    if(domain==""){
     domainInfo.style.display="";
     return false;
    }
    else{
         domainInfo.style.display="none";
          return true;
    }
}
function submitInfo(){
     if(validate()){
         var infoForm = document.getElementById("info");
         infoForm.submit();
     }
}
</ script >
< form  method = "GET"  action = "/test.html"  id = "info" >
                       域名:< input  type = "text"  class = "style5"  maxlength = "50"  name = "domain"  id = "domain"  onblur = "validate()"  />< span   style = "margin-left:10px;display:none;color:red"  id = "domainInfo" >请输入域名!</ span ></ p >
                       < input  type = "button"   class = "button glow button-rounded button-flat" >
</ form >

如果不输入就点击‘确定’便无法提交,如下图:

110620479.jpg





本文转自 baiying 51CTO博客,原文链接:,如需转载请自行联系原作者
目录
相关文章
|
移动开发 前端开发 Android开发
前端html input =“file“ ios/安卓解决无法选择图库/拍照问题
前端html input =“file“ ios/安卓解决无法选择图库/拍照问题
1621 0
|
4月前
|
Web App开发 移动开发 HTML5
HTML中input标签的23种type类型
HTML中input标签的23种type类型
|
5月前
HTML <input> 标签的 autocomplete 属性
HTML <input> 标签的 autocomplete 属性
29 0
|
5月前
|
JavaScript 前端开发 数据安全/隐私保护
HTML中input常用的type属性及使用场景
HTML中input常用的type属性及使用场景
79 0
|
7月前
|
JavaScript
使用 HTML input 元素上传本地文件,在服务器端打印出上传的内容
使用 HTML input 元素上传本地文件,在服务器端打印出上传的内容
74 0
|
7月前
|
移动开发 HTML5
修改HTML5 input placeholder 颜色及修改失效的解决办法
修改HTML5 input placeholder 颜色及修改失效的解决办法
|
9月前
HTML中<button />和<input type=“button“/>的区别
HTML中<button />和<input type=“button“/>的区别
52 0
|
9月前
|
移动开发 HTML5
Html5中的input标签之多少
Html5中的input标签之多少
67 0