我已经开始混.net版了,有料会发布在那,当然我的blog里也会有的~

解决方案 »

  1.   

    to  LCKKING:不索取并不是必须要写点什么,大家尽自己能力做力所能及的事情,为什么中国就没有好的开源土壤?为什么开源在中国收到的bug测试最少?为什么大家只忙自己的事
    to eglic(圪圪):你可以提点意见,测试几个bug,像usage内置的表达式有些我都没写那么严格
      

  2.   

    接分~_~http://dooit.3322.org/checkform/demo1.htm 打不开 :(
      

  3.   

    打不开的到这下载吧
    http://www.cnblogs.com/Files/ttyp/checkform.rar
      

  4.   

    偶的代码比较乱,刚完成的,没有整理
    =================================
    /*****************************************************************************************
    通用检查表单函数
    bool ValidateForm(FormObject)
    调用方法 <Form onsubmit="JavaScript:ValidateForm(this)">
    <input type="表单域类型"
       name="表单域名称"
       id="元素ID"
       required="是否必填"
       datatype="数据类型"
       fieldname="表单域的显示名称"
       aliesobject="关联的表单域名称"
       aliestype="关联类型"
       maxlength="最大长度"
       minlength="最小长度" />
    表单域类型 text / password
    关联类型 :等于(eq)/大于(gt)/小于(lt)/不等于(neq)/不大于(ngt)/不小于(nlt)
    *****************************************************************************************/
    function isLeapYear(iy){return (iy % 400 == 0 || (iy % 4 == 0 && iy % 100 != 0));}
    function getDayOfMonth(iy,im){
    if(im==1 || im==3 || im==5 || im==7 || im==8 || im==10 || im==12)
    return 31;
    if(im==2){
    if(isLeapYear(iy))
    return 28;
    else
    return 29;
    }
    return 30;
    }
    String.prototype.toDate=function(){
    var sy='';
    var sm='';
    var sd='';
    if((/^(\d{4})[\-\/年](\d{1,2})[\-\/月](\d{1,2})[日]?$/gi).test(this)){
    sy=RegExp.$1;
    sm=RegExp.$2;
    sd=RegExp.$3;
    }else if((/^\d{8}$/gi).test(this)){
    sy=this.substr(0,4);
    sm=this.substr(4,2);
    sd=this.substr(6,2);
    }else{
    throw new Error(0,'格式错误')
    }
    sy=sy.replace(/^0+/gi,'')+'0';
    sm=sm.replace(/^0+/gi,'')+'0';
    sd=sd.replace(/^0+/gi,'')+'0';
    var iy=parseInt(sy)/10;
    var im=parseInt(sm)/10;
    var id=parseInt(sd)/10;
    if(iy<1900 || im>2100) throw new Error(1,'年份'+sy+'超出范围');
    if(im<1 || im>12) throw new Error(1,'月份'+sm+'超出范围');
    if(id<1 || id>getDayOfMonth(iy,im)) throw new Error(1,'日期'+sd+'超出范围');
    return new Date(iy,im,id);
    }
    //ValidateForm要用到的Class,用来包装一个HTML元素
    function __ValidateElement(){
    this.tagName=''; //元素标签
    this.type=''; //元素类型
    this.value=''; //元素的值
    this.required=false; //是否必填
    this.dataType=''; //数据类型
    this.fieldName=''; //表单域显示名称
    this.aliesObject=null; //关联字段对象
    this.aliesName=''; //关联字段的显示名称
    this.aliesType=''; //关联类型
    this.dataFormat=null; //自定义数据格式
    this.minLength=0;
    ////////////////////////////////////////////////
    this.id='';
    this.name='';
    this.element=null;
    this.form=null;
    ////////////////////////////////////////////////
    this.eMessage='';
    ///////////////////////初始化///////////////////
    if(arguments.length!=1){
    throw new Error(1,'创建__ValidateElement对象时未指定任何参数!');
    }else{
    if(typeof(arguments[0])!='object'){
    throw new Error(2,'创建__ValidateElement对象时指定的参数无效!');
    }else{
    try{
    var p=arguments[0].tagName;
    }catch(e){
    throw new Error(3,'创建__ValidateElement对象时指定的参数不是HTMLElement对象!');
    }finally{
    this.tagName=p.toLowerCase();
    }
    if(this.tagName!='input'){ //这个Class只对INPUT标签有效
    throw new Error(4,'__ValidateElement对象只对INPUT对象有效!');
    }else{
    this.id=arguments[0].uniqueID;
    this.name=arguments[0].name;
    this.element=arguments[0];
    this.form=arguments[0].form;
    ////////////////////////////////////////////////
    this.type=arguments[0].type.toLowerCase();
    this.value=arguments[0].value.trim();
    this.minLength=arguments[0].getAttribute('minlength');
    if(this.minLength==null){
    this.minLength=0;
    }else if(!(/^\d+$/gi).test(this.minLength)){
    this.minLength=0;
    }else{
    this.minLength=parseInt(this.minLength);
    }
    this.required=arguments[0].getAttribute('required');
    this.required=(this.required!=null); //如果指定了required属性,不管值是多少,它都是必填字段
    if(this.type=='password') this.required=true; //如果是密码域则必填
    this.dataType=arguments[0].getAttribute('datatype');
    if(this.dataType==null) this.dataType=''; else this.dataType=this.dataType.trim().toLowerCase();
    this.dataFormat=arguments[0].getAttribute('dataformat');
    if(this.dataFormat!=null){
    try{
    this.dataFormat=new RegExp(this.dataFormat,'gi');
    }catch(e){
    throw new Error(0,this.fieldName+'域使用了非法的正则表达式');
    }
    }
    this.fieldName=arguments[0].getAttribute('fieldname');
    if(this.fieldName==null) this.fieldName=arguments[0].name;
    this.aliesObject=arguments[0].getAttribute('aliesobject');
    if(this.aliesObject!=null){
    if(this.aliesObject==this.name)
    throw new Error(5,this.fieldName+'域自身关联');
    this.aliesObject=this.form.elements[this.aliesObject];
    if(this.aliesObject==null || typeof(this.aliesObject)!='object'){
    throw new Error(6,this.fieldName+'域指定的关联字段无效');
    }else{
    this.aliesName=this.aliesObject.getAttribute('fieldname');
    if(this.aliesName==null){
    this.aliesName=this.aliesObject.name;
    }
    }
    }
    this.aliesType=arguments[0].getAttribute('aliestype');
    if(this.aliesType==null){
    if(this.aliesObject!=null){
    throw new Error(7,this.fieldName+'域指定的关联字段确没指定关联类型');
    }
    }else{
    if(this.aliesObject==null){
    throw new Error(8,this.fieldName+'域指定的关联类型却没指定关联字段');
    }else{
    this.aliesType=this.aliesType.toLowerCase().trim();
    if(!this.aliesType.inList('eq','gt','lt','neq','ngt','nlt')){
    throw new Error(9,this.fieldName+'域指定了不可识别关联类型');
    }/*
    if(this.dataType=='' || this.dataType=='userdefine'){
    throw new Error(9,this.fieldName+'域指定了关联域时必须指定数据类型且不支持自定义的数据类型');
    }*/
    }
    }//if(this.aliesType==null){
    }//if(this.tagName!='input'){
    }//if(typeof(arguments[0])!='object'){
    }//if(arguments.length!=1){
    //标记错误
      

  5.   

    this.__MarkError=function (){
    this.element.focus();
    this.element.select();
    alert(this.eMessage.replace(/\%1/gi,this.fieldName).replace(/\%2/gi,this.aliesName));
    }
    //检查数据
    this.__Validate = function (){
    if(this.value.length<this.minLength){
    this.eMessage='%1域至少需要填写'+this.minLength.toString()+'个字符';
    return false;
    }
    if(this.dataType=='userdefine'){ //先检查是否自定义规则
    if(this.dataFormat==null){
    throw new Error(10,this.fieldName+'域为自定义数据类型但没有指定数据格式');
    }else{
    if(this.dataFormat.test(this.value)){
    return true;
    }else{
    this.eMessage='您在%1域内输入的数据与指定的数据格式不匹配';
    return false;
    }
    }
    }//开始检查预定义规则
    switch(this.dataType){
    case 'year':
    if(!(/^\d{4}$/gi).test(this.value)){
    this.eMessage='%1域不是不是正确的年份数字';
    return false;
    }else{
    var p=parseInt(this.value);
    if(p<1900 || p>2100){
     this.eMessage='%1域的年份数字超出系统可以处理的范围';
     return false;}
    }
    break;
    case 'month':
    if(!(/^\d{1,2}$/gi).test(this.value)){
    this.eMessage='%1域不是不是正确的月份数字';
    return false;
    }else{
    var p=parseInt(this.value);
    if(p<1 || p>12) {
    this.eMessage='%1域的月份数字错误';
    return false;}
    }
    break;
    case 'day':
    if(!(/^\d{1,2}$/gi).test(this.value)){
    this.eMessage='%1域不是不是正确的日期数字';
    return false;
    }else{
    var p=parseInt(this.value);
    if(p<1 || p>31){
    this.eMessage='%1域的日期数字错误';
    return false;}
    }
    break;
    case 'age':
    if(!(/^\d{1,2}$/gi).test(this.value)){
    this.eMessage='%1域不是不是正确的年龄数字';
    return false;
    }else{
    var p=parseInt(this.value);
    if(p<1 || p>130){
    this.eMessage='%1域的年龄数字错误';
    return false;}
    }
    break;
    case 'integer':
    if(!(/^([\-\+]?)(\d+)$/gi).test(this.value)){
    this.eMessage='%1域不是正确的整数格式';
    return false;
    }
    break;
    case 'money': //货币和数字是一样的
    case 'numeric':
    if(!(/^[\+\-]?\d+\.?\d*$/gi).test(this.value)){
    this.eMessage='%1域不是正确的数字格式';
    return false;
    }
    break;
    case 'date':
    try{
    var pdt=this.value.toDate();
    }catch(e){
    this.eMessage='%1域不是正确的日期格式:\n          '+e.description;
    return false;
    }
    break;
    case 'time':
    if(!(/^(\d+)(\:)(\d+)(\:)(\d+)$/gi).test(this.value)){
    this.eMessage='%1域不是正确的时间格式';
    return false;
    }else{
    var sh=RegExp.$1;
    var sm=RegExp.$3;
    var ds=RegExp.$5;
    var ih=parseInt(sh);
    var im=parseInt(sm);
    var is=parseInt(ss);
    if(ih<0 || ih>23 || im<0 || im>59 || is<0 || is>59){
    this.eMessage='%1域的时间格式错误';
    return false;
    }
    }
    break;
    case 'email':
    if(!(/^[a-z][a-z0-9\.\_]+\@[a-z0-9\-\.]+$/gi).test(this.value)){
    this.eMessage='%1域不是正确的电子邮件地址';
    return false;
    }
    break;
    case 'pin':
    if(this.value.length==15 && (/^\d{15}$/gi).test(this.value)){
    var sy=this.value.substr(6,2);
    var sdt=this.value.substr(6,6);
    sy=sy.replace(/^0+/gi,'');var iy=parseInt(sy);
    if(iy<=5) sdt='20'+sdt; else sdt='19'+sdt;
    try{
    var ptmp=sdt.toDate();
    }catch(e){
    this.eMessage='%1域不是正确的身份证号码格式';
    return false;
    }
    }else if(this.value.length==18 && (/^\d{18}$/gi).test(this.value)){
    var sdt=this.value.substr(6,8);
    try{
    var ptmp=sdt.toDate();
    }catch(e){
    this.eMessage='%1域不是正确的身份证号码格式';
    return false;
    }
    }else{
    this.eMessage='%1域不是正确的身份证号码格式';
    return false;
    }
    break;
    case 'zip':
    if(!(/^\d{6}$/gi).test(this.value)){
    this.eMessage='%1域不是正确的邮政编码格式';
    return false;
    }
    break;
    case 'userid':
    if(!(/^[a-z][a-z0-9\-\_\.\@]+$/gi).test(this.value)){
    this.eMessage='%1域不是正确的用户ID格式';
    return false;
    }
    break;
    case 'mobile':
    if(!(/^13\d{9}$/gi).test(this.value)){
    this.eMessage='%1域不是正确的手机号码格式';
    return false;
    }
    break;
    case '': //跳过未设置数据类型的情况,以便继续检查关联
    break;
    default:
    throw new Error(11,this.fieldName+'域指定了无法识别的数据类型');
    break;
    }//开始检查关联
    if(this.aliesObject!=null){
    switch(this.dataType){
    case 'year':
    case 'month':
    case 'day':
    case 'integer':
    case 'money':
    case 'numeric':
    try{
    var v1=parseFloat(this.value);
    var v2=parseFloat(this.aliesObject.value);
    }catch(e){
    this.eMessage='检查%1域的关联时发生错误\n   '+e.description;
    return false;
    }
    break;
    case 'date':
    try{
    var v1=this.value.toDate().valueOf();
    var v2=this.aliesObject.value.trim().toDate().valueOf();
    }catch(e){
    this.eMessage='检查%1域的关联时发生错误\n   '+e.description;
    return false;
    }
    break;
    default:
    //throw new Error(12,this.fieldName+'域的数据类型不支持域关联校验功能');
    //检查密码功能
    var v1=this.value;
    var v2=this.aliesObject.value;
    }
    //检查是否有错误
    if(v1!=v1){
    this.eMessage='%1的值与指定的数据类型不匹配'
    return false;
    }
    if(v2!=v2){
    this.eMessage='%2的值与指定的数据类型不匹配'
    return false;
    }
    //
    switch(this.aliesType){
    case 'eq':
    if(v1!=v2){
    this.eMessage='%1的值必须等于%2的值';
    return false;
    }
    break;
    case 'gt':
    if(v1<=v2){
    this.eMessage='%1的值必须大于%2的值';
    return false;
    }
    break;
    case 'lt':
    if(v1>=v2){
    this.eMessage='%1的值必须小于%2的值';
    return false;
    }
    break;
    case 'neq':
    if(v1==v2){
    this.eMessage='%1的值不能等于%2的值';
    return false;
    }
    break;
    case 'ngt':
    if(v1>v2){
    this.eMessage='%1的值不能大于%2的值';
    return false;
    }
    break;
    case 'nlt':
    if(v1<v2){
    this.eMessage='%1的值不能小于%2的值';
    return false;
    }
    break;
    default :
    throw new Error(12,this.fieldName+'域指定了不能识别的关联类型');
    }
    }
    }
    //综合检查
    this.Validate=function (){
    this.eMessage='';
    if(this.required){ //必要检查
    if(this.value.length<1){
    this.eMessage='%1域必须填写!';
    }else{
    this.__Validate();
    }
    }else{
    this.__Validate();
    }
    if(this.eMessage!=''){
    this.__MarkError();
    return false;
    }else{ //开始检查关联
    return true;
    }
    }
    }
      

  6.   

    function ValidateForm(oFrm)
    {
    for(var iCnt=0;iCnt<oFrm.elements.length;iCnt++){
    var o=oFrm.elements[iCnt];
    var tg=o.tagName.toLowerCase(); //标签
    var ty=o.type.toLowerCase(); //元素类型
    if(tg!='input') continue;
    if(ty=='button' || ty=='reset' || ty=='submit' || ty=='image' || ty=='checkbox' || ty=='hidden') continue; //跳过的类型
    var oo=new __ValidateElement(o);
    if(ty=='radio'){ //单选框至少要选择一个
    var c=oFrm.elements[o.name];
    var f=false;
    for(var i=0;i<c.length;i++)
    {
    if(c[i].checked){
    f=true;
    break;
    }
    }
    if(!f){
    c[0].click();
    c[0].focus();
    alert('单选项 '+oo.fieldName+' 至少要选择一个!');
    oo=null;
    return f;
    }else{
    continue;
    }
    }
    var p=oo.Validate();
    oo=null;
    if(p)
    continue;
    else
    return false;
    }
    return true;
    }
      

  7.   

    调用方法写错了,呵呵
    没来得及改 <Form onsubmit="JavaScript:return ValidateForm(this)">
    ================================
    完整脚本在这里   http://www.i-love-mm.com/default.js
    测试页面是这个   http://www.i-love-mm.com/testForm.html
      

  8.   

    支持一下。
    在php里有php.MVC的框架包含这样类似的东西做验证,呵呵
      

  9.   

    发现一个bug,在.net里如果同一个form下,一个按钮需要验证,一个按钮不需要验证,以前解决不了,不过刚才已经修复了,解决办法是不需要验证的按钮加上check=false属性,顺便加强了ip验证,已经下载请重新下一个
      

  10.   

    ttyp教训的极是!以后努力学习,为大家作点贡献
      

  11.   

    我的blog里面写过一个正则的原理,把正则的起源说了一下的