代码改变世界

学习心得之:js校验

2005-03-02 16:15  胖子  阅读(873)  评论(1编辑  收藏  举报
//=================================================
//
//
    验证功能的    javascript
//
//
   最后修改日期: 2005/02/28
//
//
=================================================

//////////////////////////////////////////////////////
//
 判断是否闰年 
//
 参数 intYear 代表年份的值 
//
 return   true: 是闰年   
//
         false: 不是闰年 
//
function LeapYear(intYear) 
{
    
if (intYear % 100 == 0)
    {
        
if (intYear % 400 == 0) { return true; }
    } 
    
else
    { 
      
if ((intYear % 4== 0) { return true; } 
    } 
    
return false


//////////////////////////////////////////////////////
//
 验证日期
//
 
function checkdate(TextID) 
{
    
var flag = true;
    
var searchStr = /^[0-9]{4}-(0[1-9]|[1-9]|1[0-2])-((0[1-9]|[1-9])|1[0-9]|2[0-9]|3[0-1])$/
    
    
if!searchStr.test(TextID.value) )
    {
        
if(""==TextID.value)
        {}
        
else
        {
            TextID.value 
= "";
            alert(
"您输入的日期格式错误!");
        }
    }
    
else 
    { 
        
var getdate = TextID.value;
        
        
// 获得年 
        var year=getdate.substr(0,getdate.indexOf('-'));
        
// 下面操作获得月份
        var transition_month=getdate.substr(0,getdate.lastIndexOf('-'));
        
var month=transition_month.substr(transition_month.lastIndexOf('-')+1,transition_month.length);
        
// 下面操作获得日期 
        var day=getdate.substr(getdate.lastIndexOf('-')+1,getdate.length);
        
        
if (month.indexOf('0')==0
        { 
            month
=month.substr(1,month.length);
        }
        
if (day.indexOf('0')==0
        { 
            day
=day.substr(1,day.length);
        }
        
        
// 判断2月份 
        if( month==2 )
        {
            
if (LeapYear(year)) 
            {
                
if (day>29 || day<1)
                    flag
=false;
            }
            
else
            {
                
if (day>28 || day<1)
                    flag
=false;
            }
        }
        
// 4,6,9,11月份日期不能超过30 
        if( (month==4 || month==6 || month==9 || month==11&& (day>30) )
        {
            flag
=false;
        }
    }

    
if ( flag==false ) 
    { 
        TextID.value 
= "";
        alert(
"您输入的日期不合法!"); 
    }
}

/////////////////////////////////////////////////
//
 验证时间
//
 
function checktime(TextID) 

    
var flag = true;
    
var searchStr = /^[0-9]{4}-(0[1-9]|[1-9]|1[1-2])-((0[1-9]|[1-9])|1[0-9]|2[0-9]|3[0-1]) ((0[1-9]|[1-9])|1[0-9]|2[0-4]):((0[1-9]|[1-9])|[1-5][0-9]):((0[1-9]|[1-9])|[1-5][0-9])$/
    
if!searchStr.test(TextID.value) )
    {
        
if(""==TextID.value)
        {}
        
else
        {
            TextID.value 
= "";
            alert(
"您输入的日期时间格式错误!");
         }
    }
    
else 
    { 
        
var getdate = TextID.value;
        
        
// 获得年 
        var year=getdate.substr(0,getdate.indexOf('-'));
        
// 下面操作获得月份
        var transition_month=getdate.substr(0,getdate.lastIndexOf('-'));
        
var month=transition_month.substr(transition_month.lastIndexOf('-')+1,transition_month.length);
        
// 下面操作获得日期 
        var day=getdate.substr(getdate.lastIndexOf('-')+1,getdate.length);
        
        
if (month.indexOf('0')==0
        { 
            month
=month.substr(1,month.length);
        }
        
if (day.indexOf('0')==0
        { 
            day
=day.substr(1,day.length);
        }
        
        
// 判断2月份 
        if( month==2 )
        {
            
if (LeapYear(year)) 
            {
                
if (day>29 || day<1)
                    flag
=false;
            }
            
else
            {
                
if (day>28 || day<1)
                    flag
=false;
            }
        }
        
// 4,6,9,11月份日期不能超过30 
        if( (month==4 || month==6 || month==9 || month==11&& (day>30) )
        {
            flag
=false;
        }
    }

    
if ( flag==false ) 
    { 
        TextID.value 
= "";
        alert(
"您输入的日期不合法!"); 
    }


/////////////////////////////////////////////////
//
 数字输入控制
//
function NumCheck(obj)
{
    
if(obj.value=="")
    {
    }
    
else
    {
        
if (!isNumeric(obj.value))
        {
            alert(
"请输入整数!");
            obj.focus();
            obj.value 
= "";
            
return (false);
        }
        
else
        {    
        }
    }    
}

/////////////////////////////////////////////////
//
 判断是否是数字的函数
//
function isNumeric(strNumber)

    
//return (strNumber.search(/^(-|\+)?\d+(\.\d+)?$/) != -1); 
    return (strNumber.search(/^(\d+)?$/!= -1); 


/////////////////////////////////////////////////
//
 验证Email地址
//
function checkEmail(TextID)
{
    
var searchStr = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
    
if!searchStr.test(TextID.value) )
    {
        
if(""==TextID.value)
        {}
        
else
        {
            TextID.value 
= "";
            alert(
"Email 地址格式错误!");
        }
    }
}

/////////////////////////////////////////////////
//
 验证电话号码
//
function checkPhone(TextID)
{

    
var searchStr = /(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/
    
if!searchStr.test(TextID.value) )
    {
        
if(""==TextID.value)
        {}
        
else
        {
            TextID.value 
= "";
            alert(
"电话号码格式错误!");
        }
    }


//-------------------------------------  The end   -----------------------------------------------