通用控件校验函数


//通过控件名字得到object

function MM_findObj_(n, d)

{

    var p,i,x; 
    if(!d)

        d=document;
    if((p=n.indexOf("?"))>0&&parent.frames.length)

    {

        d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);

    }

    if( !(x=d[n]) && d.all )

        x=d.all[n];

    for (i=0;!x&&i<d.forms.length;i++)

        x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++)

        x=MM_findObj_(n,d.layers[i].document);

    return x;

}
//控件数据校验

function validate()

{

      var i,myValue1,myValue2,myValue3,myValue4,myValue5,errors='';
     var val,val1;

      var controlAttribute,putInfo1,putInfo2;
      var args = validate.arguments;

      var checkMailAddress = /w@w{3,}.w{3,}/;  //创建正则表达式校验邮件地址对象
     
      var checkInteger = /^[+-]?d+$/;  //创建正则表达式校验整数对象

      var checkFloat = /^[+-]?d+(.d+)?$/;  //创建正则表达式校验浮点数对象
    for (i=0; i<(args.length-2); i+=3)

    {

        controlAttribute = args[i];

        putInfo1 = args[i+1];       

        putInfo2 = args[i+2];

        if( controlAttribute.indexOf('D')>=0 || controlAttribute=='Y' )

            val = MM_findObj_( putInfo1+"year" );

        else

            val = MM_findObj_( putInfo1 );

        //如果控件不存在,直接跳过

        if( !val )

            continue;

        myValue1 = val.value;
       
        //对非空的处理   

        if( controlAttribute.indexOf('R') >= 0 )

        {           

            if( controlAttribute.indexOf('D8')>=0 )

            {

                val = MM_findObj_( putInfo1+'year' );

                myValue1 = MM_findObj_( putInfo1+'year' ).value;

                myValue2 = MM_findObj_( putInfo1+'monthfrom' ).value;

                myValue3 = MM_findObj_( putInfo1+'monthto' ).value;

                if( myValue1 == '' || myValue2 == '' || myValue3 == '' )

                {

                    errors = putInfo2+'不能为空';

                    break;

                }               

            }

            else if( controlAttribute.indexOf('D')>=0 )

            {

                val = MM_findObj_( putInfo1+'year' );

                myValue1 = MM_findObj_( putInfo1+'year' ).value;

                myValue2 = MM_findObj_( putInfo1+'month' ).value;

                val1 = MM_findObj_( putInfo1+'day' );

                if( val1 )

                    myValue3 = MM_findObj_( putInfo1+'day' ).value;

                else

                    myValue3 = MM_findObj_( putInfo1+'date' ).value;

                   

                if( myValue1 == '' || myValue2 == '' || myValue3 == '' )

                {

                    errors = putInfo2+'不能为空';

                    break;

                }               

            }

            else if( controlAttribute.indexOf('S')>=0 )

            {

                val = MM_findObj_( putInfo1 );

                myValue1 = MM_findObj_( putInfo1 ).value;

                if( myValue1 == '#' )

                {

                    errors = '请选择'+putInfo2;

                    break;

                }

            }

            else

            {

                val = MM_findObj_( putInfo1 );

                myValue1 = MM_findObj_( putInfo1 ).value;

                if( myValue1 == '' )

                {

                    errors = putInfo2+'不能为空';

                    break;

                }

            }
        }    
         //对有效期的处理

         if( controlAttribute=='Y' )

         {

             val = MM_findObj_( putInfo1+'year' );

             val1 = MM_findObj_( putInfo1+'day' );

             if( val1 )

             {

                 myValue1 = new Date (MM_findObj_( putInfo1+"year" ).value , MM_findObj_( putInfo1+"month" ).value , MM_findObj_( putInfo1+"day" ).value);

                 myValue2 = new Date (MM_findObj_( putInfo2+"year" ).value , MM_findObj_( putInfo2+"month" ).value , MM_findObj_( putInfo2+"day" ).value);

             }

             else

             {

                 myValue1 = new Date (MM_findObj_( putInfo1+"year" ).value , MM_findObj_( putInfo1+"month" ).value , MM_findObj_( putInfo1+"date" ).value);

                 myValue2 = new Date (MM_findObj_( putInfo2+"year" ).value , MM_findObj_( putInfo2+"month" ).value , MM_findObj_( putInfo2+"date" ).value);

             }

                if( myValue1 > myValue2 )

            {

                errors = '有效期结束时间不能大于开始时间';   

                break;   

            }

         }
        
         //对其他属性的处理

         if( myValue1 != '' )

         {    
            
             if ( controlAttribute.indexOf( 'I' ) >= 0 )

             {

                 if( !checkInteger.test(myValue1) )

                {

                    errors = putInfo2+'只能是整数';

                    break;

                }  

             }

             else if(  controlAttribute.indexOf('F') >= 0 )

            {

                if( !checkFloat.test(myValue1) )

                {

                    errors = putInfo2+'只能是实数';

                    break;

                }

            }           

            else if( controlAttribute.indexOf('D8') >= 0 )

            {

                myValue4 = new Date ( myValue1 , MM_findObj_( putInfo1+'monthfrom' ).value ,'01');

                myValue5 = new Date ( myValue1 , MM_findObj_( putInfo1+'monthto' ).value ,'01');

                if( myValue1.length !=4 || !checkDateTime( myValue1 +'-'+MM_findObj_( putInfo1+'monthfrom' ).value+'-'+'01' ) || !checkDateTime( myValue1 +'-'+MM_findObj_( putInfo1+'monthto' ).value+'-'+'01' ))

                {

                    errors = putInfo2+'时间不正确';

                    break;

                }

                else if( myValue4 > myValue5 )

                {

                    errors = putInfo2+'结束时间大于开始时间';

                    break;   

                }

                else if ( myValue1<1900 || myValue1>2100 )

                {

                    errors = putInfo2+'不能小于1900年,大于2100年';

                    break;

                }                        

            }

                else if( controlAttribute.indexOf('D')>=0 )

            {

                val1 =  MM_findObj_( putInfo1+'day' );

                myValue4 = '';

                if( val1 )

                    myValue4 = MM_findObj_( putInfo1+'day' ).value;

                else

                    myValue4 = MM_findObj_( putInfo1+'date' ).value;

               

                if( myValue1.length!=4 || !checkDateTime( myValue1+'-'+MM_findObj_( putInfo1+'month' ).value+'-'+myValue4 ) )

                {

                    errors = putInfo2+'时间不正确';

                    break;

                }

                else if ( myValue1<1900 || myValue1>2100 )

                {

                    errors = putInfo2+'不能小于1900年,大于2100年';

                    break;

                }

            } 

            else if( controlAttribute.indexOf('E')>=0 )

            {                   

                if ( !checkMailAddress.test(myValue1) )

                {                           

                    errors = putInfo2+'不是邮件地址';

                    break;                           

                }

            }

            else if( controlAttribute.indexOf('N')>=0  )

            {

                if ( !checkInteger.test(myValue1) )

                {

                    errors = putInfo2+'不正确';

                    break;

                }

                else if ( myValue1<1900 || myValue1>2100 )

                {

                    errors = putInfo2+'不能小于1900年,大于2100年';

                    break;

                }               
            }

        }
      
        //长度判断

        if( controlAttribute.indexOf('L|') >= 0 && controlAttribute.indexOf('L|') < controlAttribute.indexOf('-') )

        {                   

            val = MM_findObj_( putInfo1 );

            myValue4 = controlAttribute.substring(controlAttribute.indexOf("L|")+2,controlAttribute.indexOf('-'));

            myValue5 = controlAttribute.substring(controlAttribute.indexOf("-")+1,controlAttribute.length);
                      
            if( myValue5.indexOf('|') < 0 )

                break;
               
            myValue5 = myValue5.substring(0,myValue5.indexOf('|'));
        
            if ( myValue1.length < myValue4 || myValue1.length > myValue5 )

            {                           

                errors = putInfo2+'长度不正确';

                break;                           

            }

        }

    }   

      if ( errors != '' )

      {

          alert(errors);

          val.focus();

      }     

      document.returnValue = (errors == '');

}


function checkDateTime( str )

{

    var reg = /^(d{1,4})-(d{1,2})-(d{1,2})$/; //创建正则表达式校验时间对象

    var r = str.match(reg);

   

    if(r==null)

        return false;

    else

    {

        var d= new Date(r[1], --r[2],r[3]);        

        if(d.getFullYear()!=r[1])

            return false;

        if(d.getMonth()!=r[2])

            return false;

        if(d.getDate()!=r[3])

            return false;

    }

    return true;

}

 这个的用法是这样的,举例


PHP代码:--------------------------------------------------------------------------------

<INPUT type="button"  name="b_splcJbxx_new"  value="test" onclick="validate('RS','splcValue.String(DM_WSZL)','文书种类','R','splcValue.String(MC_SPLC)','审批流程名称');return document.returnValue">
--------------------------------------------------------------------------------

 
 
对于每一个控件是这样定义的

需要三个参数
1.校验类型
2.控件name
3.中文含义

本文作者:



相关阅读:
oracle字段合并方法
ORACLE中的各种数据类型详细的介绍
jsp web.xml文件的作用及基本配置
利用Windows注册表来清理系统垃圾文件
java 中文字符串数组按照音序排列
jQuery EasyUI 中文API Layout(Tabs)
javascript中的作用域scope介绍
回收临时表空间ORA-03297解决-入门基础
在Linux上搭建一个简单的vsftpd服务器
PHP5.0对象模型探索之抽象方法和抽象类
JavaScript进阶教程(第三课第二部分)
PHP与ASP.NET的比较
css中用javascript判断浏览器版本
菜鸟乐园:Linux系统Shell命令介绍
快速导航
PHP MySQL HTML CSS JavaScript MSSQL AJAX .NET JSP Linux Mac ASP 服务器 SQL jQuery C# C++ java Android IOS oracle MongoDB SQLite wamp 交通频道 作文范文 马年新春短信 最经典重阳节演讲稿 踏寻梦的国度作文1200字 保护城市环境450字 找采购工作总结 BPR与权力系统的重构 与梦想一起前行 清明时节雨纷纷作文1200字 笑看岁月风云 花园小学五年级第四单元测试 暑假里,我爽歪歪550字 当代小学生读后感600字 毕业实习周记范文精选 2015大学生入党申请书总结 道歉信息 香港迪士尼乐园之旅作文500字 180°的亲情 不听老人言,吃亏在眼前作文600字 民政工作年中分析动员会讲话稿 14年教学工作总结最新范文 qq个性签名简短 我站在冬天的田野边上 死亡之約 县工会第XX次代表大会闭幕式领导讲话 最渴望的 苍华落尽江湖泪 贪吃的小猪乐乐 游七星岩200字 幽道 在远方 落花为扣 红璎枪 寒冷的世界200字 微信个性签名:人不犯我,我不犯人 考试前的烦恼作文800字 我是马良作文500字 QQ伤感说说控:你只给了她温暖,却忘了我也怕冷 获奖感言 用静默铸成一道美丽的风景 教师工作总结模板-个人工作总结_个人工作总结 2015年春季期第一次家长座谈会发言稿 不“听话”的员工未必不是好员工 雨中的一朵莲 假如我会克隆快乐500字 网店代运营合同 元旦节的作文600字 春节过年作文100字:过年包饺子 绝佳风水地 小学五年级作文1200字:二十年后回故乡之台湾高科技 那坟前开满鲜花

Copyright © 2016 phpStudy |