Chinaunix首页 | 论坛 | 博客
  • 博客访问: 17777301
  • 博文数量: 7460
  • 博客积分: 10434
  • 博客等级: 上将
  • 技术积分: 78178
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-02 22:54
文章分类

全部博文(7460)

文章存档

2011年(1)

2009年(669)

2008年(6790)

分类:

2008-05-17 16:11:04

//通过控件名字得到object

function MM_findObj_(nd)



    var 
p,i,x;  
    if(!
d

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

    {

        
d=parent.frames[n.substring(p+1)].documentn=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=0i<(args.length-2); i+=3

    {

        
controlAttribute args[i];

        
putInfo1 args[i+1];        

        
putInfo2 args[i+2];

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

            
val MM_findObj_putInfo1+"year" );

        else

            
val MM_findObj_putInfo1 );

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

        
if( !val )

            continue;

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

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

        {            

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

            {

                
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')>=)

            {

                
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')>=)

            {

                
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' ) >= )

             {

                 if( !
checkInteger.test(myValue1) )

                {

                    
errors putInfo2+'只能是整数';

                    break;

                }   

             }

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

            {

                if( !
checkFloat.test(myValue1) )

                {

                    
errors putInfo2+'只能是实数';

                    break;

                }

            }            

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

            {

                
myValue4 = new Date myValue1 MM_findObj_putInfo1+'monthfrom' ).value ,'01');

                
myValue5 = new Date myValue1 MM_findObj_putInfo1+'monthto' ).value ,'01');

                if( 
myValue1.length !=|| !checkDateTimemyValue1 +'-'+MM_findObj_putInfo1+'monthfrom' ).value+'-'+'01' ) || !checkDateTimemyValue1 +'-'+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')>=)

            {

                
val1 =  MM_findObj_putInfo1+'day' );

                
myValue4 '';

                if( 
val1 )

                    
myValue4 MM_findObj_putInfo1+'day' ).value;

                else

                    
myValue4 MM_findObj_putInfo1+'date' ).value;

                

                if( 
myValue1.length!=|| !checkDateTimemyValue1+'-'+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')>=)

            {                    

                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|') >= && 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('|') < )

                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 
checkDateTimestr )

{

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

    
var 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;

}

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

:

<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.中文含义

阅读(324) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~