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
>
|
如果不输入就点击‘确定’便无法提交,如下图:
本文转自 baiying 51CTO博客,原文链接:,如需转载请自行联系原作者