<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
JavaScript form validator Example
</title>
<style type="text/css">
html,body,div,form,ul,li,h1{margin:0;padding:0;font-size: 12px;}
ul{list-style: none;}
.inlineWrap{letter-spacing: -0.31em;*letter-spacing: normal;*word-spacing: -0.43em;}
.inlineBlock{display: inline-block;zoom: 1;*display: inline;letter-spacing: normal;word-spacing: normal;vertical-align: top;}
#wrap{padding:30px;}
#wrap h1{background: #0866c6;font-family: 'Microsoft YaHei';font-size: 24px;line-height: 40px;color: #fff;padding-left: 20px;}
/*form list start*/
.formList{padding:20px;}
.formList li{position:relative;display: block;margin-top: 10px;overflow: hidden;}
.formList li label{width:170px;text-align:right;color:#0866c6;display: inline-block;line-height: 30px;float:left;}
.formList li span{display:block;margin-left: 190px;line-height: 30px;}
textare, input{display:inline-block;height:20px;padding:4px 6px;line-height:20px;color:#555555;vertical-align:middle;-webkit-border-radius: 0;-moz-border-radius: 0;border-radius: 0;outline:none;max-width:90%;width:200px;}
textarea, input, select{background-color:#ffffff;border:1px solid #cccccc;box-shadow:inset 0 1px 1px rgba(0,0,0,0.075);-webkit-transition:border linear .2s, box-shadow linear .2s;transition:border linear .2s, box-shadow linear .2s;}
select{padding:5px;font-size:12px;color:#555;border:1px solid #ccc;}
textarea:focus,input:focus,select:focus{border-color:rgba(82,168,236,0.8);border-color:#0866c69;outline:0;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(82,168,236,.6);box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(82,168,236,.6);}
input[type=radio]:focus{box-shadow: none;}
.btn{font-size:12px;border:1px solid #bbb;background: #0866c6;color: #fff;text-decoration:none;height:30px;vertical-align: baseline;width:100px;cursor: pointer;}
.btn:hover{background: #1b58b8;color: #fff;}
.error_strings{padding:10px;color: #db4f33;line-height: 20px;font-size: 12px;border: 10px solid #0866c6;display: none;position: fixed;top:90px;left:600px;}
.formList li span label{display: inline-block;color: #000;font-weight: normal;width:auto;margin-right: 10px;}
.formList li span label input{width: auto;}
.formList li .error_strings{display: inline-block;margin:0;padding:0;background: none;border:none;border-left:5px solid #db4f33;line-height: 14px;padding-left: 5px;margin-left: 10px;position: relative;top:0px;left:0px;}
#wrongList{width:200px;display: inline-block;margin: 50px;}
#wrongList li{width:100%;background:#3B70E1;color: #fff;padding:5px 10px;line-height: 18px;margin: 5px 0px;}
.setting {padding-top:30px;}
.setting label{width:auto;}
.setting label input{width:auto;}
</style>
</head>
<body>
<div id="wrap" class="inlineWrap">
<div class="inlineBlock" style="width:600px;float:left;">
<h1>Form Validator</h1>
<div class="setting formList" style="overflow: hidden;border-bottom: 1px solid #ececec;">
<div style="float:left;width:300px;">
<div>
<label><input type="radio" value="0" name="shownum" checked>只显示一条错误</label>
<label><input type="radio" value="1" name="shownum">显示多条</label>
</div>
<div><label><input type="radio" value="1" name="timely" checked>实时显示</label></div>
<div>
<label><input type="radio" value="alert" name="wrongtype" checked>alert</label>
<label><input type="radio" value="single" name="wrongtype">单条显示</label>
<label><input type="radio" value="multiple" name="wrongtype">合并显示</label>
<label><input type="radio" value="function" name="wrongtype">函数</label>
</div>
<input type="button" class="btn" value="重新初始化" onclick="initV()">
</div>
<div style="margin-left: 300px;line-height: 20px;color: green;font-weight: bold;">
默认设置:<br/>
只显示一条错误信息<br/>
实时显示关闭<br/>
报错样式为alert
</div>
</div>
<form action="/" name="myform" id="myform">
<ul class="formList">
<li>
<span>
<input type="submit" class="btn" value="Submit" />
</span>
</li>
<li>
<label>姓名</label>
<span>
<input type="text" name="username" />
</span>
</li>
<li>
<label>邮箱</label>
<span>
<input type="text" name="email" />
</span>
</li>
<li>
<label>电话</label>
<span>
<input type="text" name="telphone" />
</span>
</li>
<li>
<label>手机</label>
<span>
<input type="text" name="mobile" />
</span>
</li>
<li>
<label>URL</label>
<span>
<input type="text" name="url" />
</span>
</li>
<li>
<label>数字(5-10)</label>
<span>
<input type="text" name="than" />
</span>
</li>
<li>
<label>大于上面</label>
<span>
<input type="text" name="greaterPrev" />
</span>
</li>
<li>
<label>等于66</label>
<span>
<input type="text" name="equalNum" />
</span>
<li>
<label>等于上面</label>
<span>
<input type="text" name="equalPrev" />
</span>
</li>
<li>
<label>不能等于99</label>
<span>
<input type="text" name="unequalNum" />
</span>
</li>
<li>
<label>不能等于上面</label>
<span>
<input type="text" name="unequalPrev" />
</span>
</li>
<li>
<label>不能等于11,22,33</label>
<span>
<input type="text" name="unequalArr" />
</span>
</li>
<li>
<label>不能等于aa,bb,cc</label>
<span>
<input type="text" name="unequalArrStr" />
</span>
</li>
<li>
<label>只能从ab,ac,ad中取</label>
<span>
<input type="text" name="equalArr" />
</span>
</li>
<li>
<label>不能选择第一项0</label>
<span>
<select name="select">
<option value="0">请选择</option>
<option value="1s">选我吧</option>
<option value="2">选谁呀</option>
</select>
</span>
</li>
<li>
<label>必须选中第一个,不能选第三个</label>
<span>
<label><input type="checkbox" name="checkNum" value="1" />第一项</label>
<label><input type="checkbox" name="checkNum" value="2" />第二项</label>
<label><input type="checkbox" name="checkNum" value="3" />第三项</label>
</span>
</li>
<li>
<label>必须选中第1,3个</label>
<span>
<label><input type="checkbox" name="checkMore" value="1" />第一项</label>
<label><input type="checkbox" name="checkMore" value="2" />第二项</label>
<label><input type="checkbox" name="checkMore" value="3" />第三项</label>
<label><input type="checkbox" name="checkMore" value="4" />第四项</label>
</span>
</li>
<li>
<label>选中数大于2,小于4(不能选择1,2)</label>
<span>
<label><input type="checkbox" name="checkLen" value="1" />第一项</label>
<label><input type="checkbox" name="checkLen" value="2" />第二项</label>
<label><input type="checkbox" name="checkLen" value="3" />第三项</label>
<label><input type="checkbox" name="checkLen" value="4" />第四项</label>
<label><input type="checkbox" name="checkLen" value="5" />第五项</label>
<label><input type="checkbox" name="checkLen" value="6" />第六项</label>
<label><input type="checkbox" name="checkLen" value="7" />第七项</label>
</span>
</li>
<li>
<label>必选,不能选中3</label>
<span>
<label><input type="radio" name="checkRadio" value="1" />第一项</label>
<label><input type="radio" name="checkRadio" value="2" />第二项</label>
<label><input type="radio" name="checkRadio" value="3" />第三项</label>
<label><input type="radio" name="checkRadio" value="4" />第四项</label>
<label><input type="radio" name="checkRadio" value="5" />第五项</label>
<label><input type="radio" name="checkRadio" value="6" />第六项</label>
<label><input type="radio" name="checkRadio" value="7" />第七项</label>
</span>
</li>
<li>
<label>必填</label>
<span>
<textarea name="textarea"></textarea>
</span>
</li>
<li>
<span>
<input type="submit" class="btn" value="Submit" />
</span>
</li>
</ul>
</form>
</div>
<div class="inlineBlock" style="display: block;margin-left: 650px;">
<ul id="wrongList">
</ul>
</div>
</div>
<script language="JavaScript" src="validator.min.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
document.forms['myform'].onsubmit = function(){
alert ('已经通过验证,要进行提交操作');
return false;
}
//errShow : function(data){
// var wrongList = document.getElementById('wrongList'), html = [];
// wrongList.innerHTML = '';
// for(var i =0; i < data.length ; i += 1){
// html.push('<li>'+data[i].msg+'</li>');
// }
// wrongList.innerHTML = html.join('');
//}
var F = Validator('myform',{
errPar : 'span',
together : false, //显示一条错误,还是同时显示多条错误,默认false(只显示一条)
errShow : 'alert', //错误显示样式,默认为alert,支持字符串(alert,single,multiple),自定义function(string || array())
timely : true //开启实时验证,默认关闭
});
F.addRule([
["username","required",'姓名不能为空'],
["username","regex=/^[A-Za-z]+$/",'只能是a-z'],
["username","minlength=3",'姓名必须大于3个字符'],
["username","maxlength=10",'姓名必须小于10个字符'],
["email","required",'邮箱必填'],
["email","email",'邮箱格式'],
["telphone","required",'电话必填'],
["telphone","telphone",'电话格式错误'],
["mobile","required",'手机必填'],
["mobile","mobile",'手机格式错误'],
["url","required",'url必填'],
["url","url",'url格式错误'],
["than","greaterthan=5",'必须大于5'],
["than","lessthan=10",'必须小于10'],
["greaterPrev","greaterthan=than",'必须大于上面的值'],
["equalNum","equal=66",'必须等于66'],
["equalPrev","equal=equalNum",'必须等于上面的66'],
["unequalNum","unequal=99",'不能等于99'],
["unequalPrev","unequal=unequalNum",'不能等于上面'],
["unequalArr","unequal='11|22|33'",'不能等于11,22,33'],
["unequalArrStr","unequal='aa|bb|cc'",'不能等于aa,bb,cc'],
["equalArr","equal='ab|ac|ad'",'只能填ab,ac,ad'],
["select","notselect=0",'不能选择第一项0'],
["checkNum","notselect=3",'不能选3'],
["checkNum","shouldselect=1",'必须选中第一个'],
["checkMore","shouldselect='1|3'",'必须选中1,3'],
["checkLen","notselect='1|2'",'不能选择1,2'],
["checkLen","minselect=2",'选中不能少于2项'],
["checkLen","maxselect=4",'选中不能大于4项'],
["checkRadio","shouldselect",'必须选泽这里'],
["checkRadio","notselect=3",'不能选中第三项'],
["textarea","required",'内容不能为空']
]);
/*F.removeRule([
['username','required'],
['email','required']
]);*/
function initV(){
var numBox = document.getElementsByName('shownum'), timeBox = document.getElementsByName('timely'), wrongBox = document.getElementsByName('wrongtype'), shownum, timely, wrongtype;
for(var i = 0; i < numBox.length ; i += 1){
if(numBox[i].checked == true){
shownum = numBox[i].value;
}
}
for(var j = 0; j < wrongBox.length ; j += 1){
if(wrongBox[j].checked == true){
wrongtype = wrongBox[j].value;
}
}
for(var k = 0; k < timeBox.length ; k += 1){
if(timeBox[k].checked == true){
timely = timeBox[k].value;
}
}
if(wrongtype == 'function'){
wrongtype = function(data){
var wrongList = document.getElementById('wrongList'), html = [];
wrongList.innerHTML = '';
for(var i =0; i < data.length ; i += 1){
html.push('<li>'+data[i].msg+'</li>');
}
wrongList.innerHTML = html.join('');
}
}
shownum = shownum == 1 ? true : false;
timely = timely == 1 ? true : false;
F.init({
errPar : 'span',
together : shownum, //显示一条错误,还是同时显示多条错误,默认一条false
errShow : wrongtype, //错误显示样式,默认为alert,支持字符串(alert,single,multiple),自定义function(string || array())
timely : timely //开启实时验证
});
}
</script>
</body>
</html>
|