2008年总结项目中常用到的JS验证脚本

2008年总结项目中常用到的JS验证脚本

//  ================================================================
//
 本函数用于限制文本输入框中只能输入数字"0"到"9",".","-"
//
 ================================================================
function  JHshNumberText()
{
    
if ( !(((window.event.keyCode >= 48&& (window.event.keyCode <= 57)) 
            
|| (window.event.keyCode == 13|| (window.event.keyCode == 46
            
|| (window.event.keyCode == 45)))
    
{
        window.event.keyCode 
= 0 ;
    }
    
    
//<form name=frm>
    //<input type=text name=test value="" onKeypress="JHshNumberText()">
    //<input type=button name=submit value=submit>
    //</form>    
}
 

//  ================================================================
//
 本函数用于自动将输入文本框中的内容转换成大写字符 
//
 ================================================================ 
function  JHshToUpperCase() 
{
    
if ((window.event.keyCode >= 97&& (window.event.keyCode <= 122)) 
    
{
        window.event.keyCode 
= window.event.keyCode - 32 ; 
    }
 
}


//  ================================================================ 
//
 本函数用于自动将输入文本框中的内容转换成小写字符 
//
 ================================================================
function  JHshToLowerCase() 
{
    
if ((window.event.keyCode >= 65&& (window.event.keyCode <= 90)) 
    
{
        window.event.keyCode 
= window.event.keyCode + 32 ; 
    }
 
}
  

// /截除字符串前后空格
function  JHshTrim(sString)
{
    
var strTmp ;
    strTmp 
= JHshRTrim(JHshLTrim(sString)) ;
    
return strTmp ;
}
 

//  -----------------------------------------------------------------------------------
//
本函数用于对sString字符串进行前空格截除
//
 -----------------------------------------------------------------------------------
function  JHshLTrim(sString)

    
var sStr,i,iStart,sResult = "";

    sStr 
= sString.split("");
    iStart 
= -1 ;
    
for (i = 0 ; i < sStr.length ; i++)
    
{
        
if (sStr[i] != " "
        
{
            iStart 
= i;
            
break;
        }

    }

    
if (iStart == -1
    
return "" ;}    //表示sString中的所有字符均是空格,则返回空串
    else 
    
return sString.substring(iStart) ;}
}


//  -----------------------------------------------------------------------------------
//
本函数用于对sString字符串进行后空格截除
//
 -----------------------------------------------------------------------------------
function  JHshRTrim(sString)

    
var sStr,i,iStart,sResult = "";
    sStr 
= sString.split("");
    iStart 
= -1 ;
    
for (i = sStr.length-1 ; i >= 0 ; i--)
    
{
        
if (sStr[i] != " "
        
{
            iStart 
= i;
            
break;
        }

    }
    
    
    
if (iStart == -1
    
return "" ;}    //表示sString中的所有字符均是空格,则返回空串
    else
    
return sString.substring(0,iStart+1) ;}
}



// ------------------------------------------------------------------------------------
//
判断字符串是否是日期格式,//标准格式:2004-3-29 12:05 其他格式都不允许
//
------------------------------------------------------------------------------------
function  IsDate(DateString)
{
    
if( DateString == null )
        
return false;
        
    
var Dilimeter = '-';
    
var tempY = '';
    
var tempM = '';
    
var tempD = '';
    
    
var tempH = '';
    
var tempMi = '';
    
var tempS  = '';
    
    
var tempArr1,tempArr2,tempArr3;
    
if( DateString.length < 8 && DateString.length > 18 )
        
return false;
    tempArr1 
= DateString.split(" ");
    
if( tempArr1.length != 2)
        
return false;

    tempArr2 
= tempArr1[0].split(Dilimeter);
    
if( tempArr2.length != 3 )
        
return false;
    tempY 
= tempArr2[0];
    tempM 
= tempArr2[1];
    tempD 
= tempArr2[2];

    tempArr3 
= tempArr1[1].split(':');
    
if( tempArr2.length < 2 || tempArr2.length > 3)
    
{
        
return false;
    }

    tempH 
= tempArr3[0];
    tempMi 
= tempArr3[1];
    
if( tempArr2.length == 3)
        tempS 
= tempArr3[2];

    
var tDateString;    
    
if( tempArr3.length == 2)
    
{
        tDateString 
= tempY + '/' + tempM +'/' +tempD +' '+tempH+':'+tempMi;
    }

    
else
    
{
        tDateString 
= tempY + '/' + tempM +'/' +tempD +' '+tempH+':'+tempMi+":"+tempS;
    }

    
var tempDate = new Date( tDateString );
    
if( isNaN(tempDate))
    
{
        
//alert("isNAN");
        return false;
    }


    
if( tempArr3.length == 2)
    
{
        
//2004-3-29 12:05
        if((tempDate.getUTCFullYear().toString() == tempY) &&
            (tempDate.getMonth() 
== parseInt(tempM)-1&&
            (tempDate.getDate() 
== parseInt(tempD)) && 
            (tempDate.getHours() 
== parseInt(tempH)) &&
            (tempDate.getMinutes() 
== parseInt(tempMi)))
        
{
            
return true;
        }

        
else
        
{
            
return false;
        }

    }

    
else
    
{
        
//2004-3-29 12:05:30
        if((tempDate.getUTCFullYear().toString() == tempY) &&
            (tempDate.getMonth() 
== parseInt(tempM)-1&&
            (tempDate.getDate() 
== parseInt(tempD)) && 
            (tempDate.getHours() 
== parseInt(tempH)) &&
            (tempDate.getMinutes() 
== parseInt(tempMi)) &&
            (tempDate.getSeconds() 
== parseInt(tempS)))
        
{
            
return true;
        }

        
else
        
{
            
return false;
        }

    }

    
}


// ------------------------------------------------------------------------------------
//
判断字符串日期的大小,//标准格式:2004-3-29 12:05
//
            用之前判断日期的有效性
//
 如果DateString1比DateString2时间晚,则返回true;否则false
//
------------------------------------------------------------------------------------

function  DateCompare(DateString1, DateString2)
{
    
var i;
    
var Dilimeter = '-';
    
    
var tempArr1,tempArr2;
    tempArr1 
= DateString1.split(Dilimeter);
    tempArr2 
= DateString2.split(Dilimeter);
    
    
var tDateString1 = "";    
    
for(i=0; i<tempArr1.length; i++)
    
{
        
if( i < tempArr1.length-1)
            tDateString1 
+= tempArr1[i]+"/";
        
else
            tDateString1 
+= tempArr1[i];                
    }

    
var tDateString2 = "";    
    
for(i=0; i<tempArr2.length; i++)
    
{
        
if( i < tempArr2.length-1)
            tDateString2 
+= tempArr2[i]+"/";
        
else
            tDateString2 
+= tempArr2[i];        
        
    }


    
var tempDate1 = new Date( tDateString1 );
    
var tempDate2 = new Date( tDateString2 );
    
if( isNaN(tempDate1) || isNaN(tempDate2))
    
{
        
return false;
    }


    
//2004-3-29 12:05:30
    if( tempDate1.getUTCFullYear() < tempDate2.getUTCFullYear())
        
return false;
    
if( tempDate1.getUTCFullYear() > tempDate2.getUTCFullYear())
        
return true;
    
if( tempDate1.getMonth() < tempDate2.getMonth())
        
return false;
    
if( tempDate1.getMonth() > tempDate2.getMonth())
        
return true;
    
if( tempDate1.getDate() < tempDate2.getDate())
        
return false;
    
if( tempDate1.getDate() > tempDate2.getDate())
        
return true;
    
if( tempDate1.getHours() < tempDate2.getHours())
        
return false;
    
if( tempDate1.getHours() > tempDate2.getHours())
        
return true;
    
if( tempDate1.getMinutes() < tempDate2.getMinutes())
        
return false;
    
if( tempDate1.getMinutes() > tempDate2.getMinutes())
        
return true;
    
if( tempDate1.getSeconds() < tempDate2.getSeconds())
        
return false;
    
if( tempDate1.getSeconds() > tempDate2.getSeconds())
        
return true;
    
//如果相等则返回false
    return false;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值