asfman
android developer
posts - 90,  comments - 213,  trackbacks - 0

//-----------------------------------------------------------------------------\
//JavaScript FORM validator CLASS version 0.1 alpha                                        
//这是JavaScript表单验证的类的代码的ALPHA测试版。欢迎使用!
//
//Author:Bardo    作者:祁宏
//Release Date: 2006-10-30
//参考资料:
//我佛山人 Validator v1.05
//-----------------------------------------------------------------------------\

function validator(value,ctype)
{
 this.value=value;
 this.vtype=ctype;
 this.result=false;
 switch(ctype){
  case "ISEMPTY" : {this.result = this.isEmpty();break;}
  case "ISMAIL" :  {this.result = this.isMail();break;}
  case "ISINTEGER" : {this.result = this.isInteger();break;}
  case "ISDOUBLE" : {this.result = this.isDouble();break;}
  case "ISCURRENCY" : {this.result = this.isCurrency();break;}
  case "ISZIPCODE" : {this.result = this.isZipCode();break;}
  case "ISENGLISH" : {this.result = this.isEnglish();break;}
  case "ISCHINESE" : {this.result = this.isChinese();break;}
  case "ISUSERNAME" : {this.result = this.isUsername();break;}
  case "ISCOLOR" : {this.result = this.isColor();break;}
  case "ISURL" :  {this.result = this.isURL();break;}
  case "ISPHONENUMBER" : {this.result = this.isPhoneNumber();break;}
  case "ISMOBILE" : {this.result = this.isMobile();break;}
  case "ISDATE" :  {this.result = this.isDate();break;}
  case "ISIPADDRESS" : {this.result = this.isIpAddress();break;}
  case "ISBLANKLINE" : {this.result = this.isBlankLine();break;}
  case "ISHTML" :  {this.result = this.isHTML();break;}
  case "ISIDCARDNUMBER" : {this.result = this.isIdCardNumber();break;}
 }
 
}

//去左空格;
validator.prototype.ltrim = function(){
 return this.value.replace( /^\s*/, "");
}

//去右空格;
validator.prototype.rtrim = function(){
 return this.value.replace( /\s*$/, "");
}

//去左右空格;
validator.prototype.trim = function(){
 return this.value.replace(/(^\s*)|(\s*$)/g, "");
}

//是否为空值;
validator.prototype.isEmpty = function(){
 var tmp_str = trim(this.value);
 return tmp_str.length == 0;
}

//是否有效的Email;
validator.prototype.isMail = function(){
 var tmp_str = this.trim(this.value);
 var pattern = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
 return pattern.test(tmp_str);  
}

//是否有效的数字;
validator.prototype.isNumber = function(){
 var tmp_str = trim(this.value);
 var pattern = /^\d+$/;
 return pattern.test(tmp_str);  
}

//是否有效的整数;
validator.prototype.isInteger = function(){
 var tmp_str = trim(this.value);
 var pattern = /^[-\+]?\d+$/;
 return pattern.test(tmp_str);  
}

//是否有效的双精度数;
validator.prototype.isDouble = function(){
 var tmp_str = trim(this.value);
 var pattern = /^[-\+]?\d+(\.\d+)?$/;
 return pattern.test(tmp_str);  
}

//是否有效的币值数;
validator.prototype.isCurrency = function(){
 var tmp_str = trim(this.value);
 var pattern = /^\d+(\.\d+)?$/;
 return pattern.test(tmp_str);  
}

//是否有效的邮政编码;
validator.prototype.isZipCode = function(){
 var tmp_str = trim(this.value);
 var pattern =  /^[1-9]\d{5}$/;
 return pattern.test(tmp_str);  
}

//是否为字母字串;
validator.prototype.isEnglish = function(){
 var tmp_str = trim(this.value);
 var pattern = /^[A-Za-z]+$/;
 return pattern.test(tmp_str);  
}

//是否为汉字字串;
validator.prototype.isChinese = function(){
 var tmp_str = trim(this.value);
 var pattern = /^[\u0391-\uFFE5]+$/;
 return pattern.test(tmp_str);  
}

//是否为有效的用户名;
validator.prototype.isUsername = function(){
 var tmp_str = trim(this.value);
 var pattern = /^[a-z]\w{3,}$/i;
 return pattern.test(tmp_str);  
}

//是否有效的颜色值;
validator.prototype.isColor = function(){
 var temp=this.value;
 if (temp=="") return true;
 if (temp.length!=7) return false;
 return (temp.search(/\#[a-fA-F0-9]{6}/) != -1);
}

//是否有效的链接(不仅限于HTTP,同时可FTP或其它);
validator.prototype.isURL = function(){
 var pattern= /[a-zA-z]+://[^\s]*/;
 return pattern.test(this.value);   
}

//是否有效的电话号码
validator.prototype.isPhoneNumber = function(){
 var pattern= /\d{3}-\d{8}|\d{4}-\d{7}/;
 return pattern.test(this.value);   
}

//是否有效的手机号码(支持新的159开头的号);
validator.prototype.isMobile = function(){
 var tmp_str = trim(this.value);
 var pattern = /^13[0-9]{9,9}$|159[0-9]{8,8}$/;
 return pattern.test(tmp_str);  
}

//是否为有效的日期(日期格式为YYYY-MM-DD);
validator.prototype.isDate = function()
{
 var reExp=/^(19|20)\d{2}[\-](0[1-9]|1[0-2])[\-](0[1-9]|[12][0-9]|3[01])$/;
 if (reExp.exec(this.value))
  return true;
 else
  return false;

}

//是否有效的IP地址(仅对数字型IP地址有用);
validator.prototype.isIpAddress = function(){
 var tmp_str = trim(this.value);
 var pattern = /\d+\.\d+\.\d+\.\d+/;
 return pattern.test(tmp_str);  
}

//是否为空白行;
validator.prototype.isBlankLine = function(){
 var tmp_str = trim(this.value);
 var pattern = /\n\s*\r/;
 return pattern.test(tmp_str);  
}

//是否为HTML文本;
validator.prototype.isHTML = function(){
 var tmp_str = trim(this.value);
 var pattern = /^< (\S*?)[^>]*>.*?|< .*? />$/;
 return pattern.test(tmp_str);  
}

//是否为有效的身份证号(18或15位均可)
validator.prototype.isIdCardNumber = function()
{
 var date, Ai;
 var verify = "10x98765432";
 var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
 var area = ['','','','','','','','','','','','北京','天津','河北','山西','内蒙古','','','','','','辽宁','吉林','黑龙江','','','','','','','','上海','江苏','浙江','安微','福建','江西','山东','','','','河南','湖北','湖南','广东','广西','海南','','','','重庆','四川','贵州','云南','西藏','','','','','','','陕西','甘肃','青海','宁夏','新疆','','','','','','台湾','','','','','','','','','','香港','澳门','','','','','','','','','国外'];
 var re = this.value.match(/^(\d{2})\d{4}(((\d{2})(\d{2})(\d{2})(\d{3}))|((\d{4})(\d{2})(\d{2})(\d{3}[x\d])))$/i);
 if(re == null) return false;
 if(re[1] >= area.length || area[re[1]] == "") return false;
 if(re[2].length == 12){
  Ai = this.value.substr(0, 17);
  date = [re[9], re[10], re[11]].join("-");
 }
 else{
  Ai = this.value.substr(0, 6) + "19" + this.value.substr(6);
  date = ["19" + re[4], re[5], re[6]].join("-");
 }
 if(!this.IsDate(date, "ymd")) return false;
 var sum = 0;
 for(var i = 0;i<=16;i++){
  sum += Ai.charAt(i) * Wi[i];
 }
 Ai +=  verify.charAt(sum%11);
 return (this.value.length ==15 || this.value.length == 18 && this.value == Ai);
}

//计算混合字节字串的字节长度
validator.prototype.lenB=function()
{
 return this.value.replace([^\x00-\xff]/g,"aa").length;
}

//过滤扩展名,检测是否有允许
validator.prototype.DoFilter = function(filter)
{
  return new RegExp("^.+\.(?=EXT)(EXT)$".replace(/EXT/g, filter.split(/\s*,\s*/).join("|")), "gi").test(this.value);
}

//Sample:
function IsDate(vdate)
{
 var vld = new validator(vdate,"ISDATE")
 return vld.result;
}

function doValidate(value,vtype)
{
 var vld = new validator(value,vtype)
 return vld.result;
}

 

posted on 2006-12-05 10:53 汪杰 阅读(640) 评论(1)  编辑 收藏 引用 所属分类: javascript

FeedBack:
# re: 佛山人 Validator v1.05
2008-07-26 22:49 | Bardo
看看这个新版本的,目前只能在IE中运行,老兄有意可以再转载

http://www.pisx.com/index_pss.php/pagename-product-artid-101.htm  回复  更多评论
  
只有注册用户登录后才能发表评论。

<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(15)

随笔分类(1)

随笔档案(90)

文章分类(727)

文章档案(712)

相册

收藏夹

http://blog.csdn.net/prodigynonsense

友情链接

最新随笔

搜索

  •  

积分与排名

  • 积分 - 468991
  • 排名 - 6

最新随笔

最新评论

阅读排行榜

评论排行榜