通用控件校验函数

字体大小: 中小 标准 ->行高大小: 标准
//通过控件名字得到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.中文含义

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/805.html