<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<html>
<script type="text/javascript" src="jquery.js"></script>
<style type="text/css">
</style>
<body style="height:100%;width:100%;">
<form id="myform" method="post" action="aaa.php" οnsubmit="return dm.check(this)">
<!-- <div>aaa:<input type="text" name="aaa" id="aaa" validate="q" info="q:此项为必填|e:你个狗日的电子邮箱" showOk="yes"></div> -->
<div>aaa:<input type="text" name="aaa" id="aaa" validate="s" s="3,4"></div>
<div>bbb:<input type="text" name="bbb" id="bbb" validate="q" reg="/^\d+$/" warning="此项为数字"></div>
<input type="submit" >
<input type="button" value="ok" οnclick="dm.check(this)">
</form>
<!--form id="myform2" method="post" action="aaa.php" οnsubmit="return dm.check(this)">
<div>aaa:<input type="text" name="aaa" id="aaa" validate="q|a|k|z" info="sss:ccc|z:ddd"></div>
<div>bbb:<input type="text" name="bbb" id="bbb" validate="q|e" info="sss:ccc|z:ddd"></div>
<input type="submit" >
<input type="button" value="ok" οnclick="dm.check(this)">
</form -->
</body>
<script type="text/javascript">
var dm={
//通用验正工具
//author:pengshihong
//2015/2
regArray:{
Require : /.+/,
Email : /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
Phone : /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/,
Mobile : /^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$/,
Url : /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/,
Currency : /^\d+(\.\d+)?$/,
Number : /^\d+$/,
Zip : /^[1-9]\d{5}$/,
QQ : /^[1-9]\d{4,8}$/,
Integer : /^[-\+]?\d+$/,
Double : /^[-\+]?\d+(\.\d+)?$/,
English : /^[A-Za-z]+$/,
Chinese : /^[\u0391-\uFFE5]+$/,
Username : /^[a-z]\w{3,}$/i,
UnSafe : /^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/
},
getInfo:function(obj){
var infoArr=[];
if(typeof($(obj).attr("info"))!="undefined"){
var tempArr=$(obj).attr("info").split("|");
for(var j=0;j<tempArr.length;j++){
var dd=[];
var dd=tempArr[j].split(":");
infoArr[dd[0]]=dd[1];
}
}
return infoArr;
},
setInfo:function(obj,str,defaultInfo){
var message=defaultInfo;
obj.after("<span tag=\"info\"></span>");
if(typeof(str)!="undefined"){
message=str;
}
obj.next("[tag=info]").html(message);
},
check:function(a){
point=this;
var st=true;
$(a).closest("form").find("input:text").each(function(){
tempst=true;
infoArr=[];
that=$(this);
infoArr=point.getInfo(that);
var k=that.attr("validate");
if(typeof(k)!="undefined"){
var arr=[];
arr=k.split("|");
for(var i=0;i<arr.length;i++){
that.next("[tag='info']").remove();
//必填
if(arr[i]=="q"){
if(that.val()==""){
var message="此项不能为空";
point.setInfo(that,infoArr['q'],message);
st=false;
tempst=false;
break;
}
}
if(arr[i]=="e"){
if(!dm.regArray.Email.test(that.val())){
var message="email格式不正确!";
point.setInfo(that,infoArr['e'],message);
st=false;
tempst=false;
break;
}
}
//中文
if(arr[i]=="z"){
if(!dm.regArray.Chinese.test(that.val())){
var message="必须为中文!";
point.setInfo(that,infoArr['z'],message);
st=false;
tempst=false;
break;
}
}
if(arr[i]=="s"){
if(that.attr("s")!="undefined"){
that.next("[tag='info']").remove();
var s=that.attr("s").split(",");
if(that.val().length<s[0] || that.val().length>s[1]){
st=false;
tempst=false;
that.after("<span tag=\"info\"></span>");
that.next("[tag=info]").html("长度在"+s[0]+"-"+s[1]+"个字符之间");
break;
}
}
}
}
if(tempst!=false){
that.next("[tag='info']").remove();
//用户自定义正则
var reg=that.attr("reg");
reg=eval(reg);
var warning=that.attr("warning");
if(typeof(reg)!="undefined" && typeof(warning)!="undefined"){
if(!reg.test(that.val())){
st=false;
that.after("<span tag=\"info\"></span>");
that.next("[tag=info]").html(warning);
}
}
}
}
})
return st;
}
}
</script>
</html>