javascript通用函數庫

來源:互聯網
上載者:User

/*

-------------- 函數檢索 --------------
trim函數:                         trim() lTrim() rTrim()
校正字串是否為空白:                 checkIsNotEmpty(str)
校正字串是否為整型:               checkIsInteger(str)
校正整型最小值:                    checkIntegerMinValue(str,val)
校正整型最大值:                    checkIntegerMaxValue(str,val)
校正整型是否為非負數:               isNotNegativeInteger(str)
校正字串是否為浮點型:             checkIsDouble(str)
校正浮點型最小值:                  checkDoubleMinValue(str,val)
校正浮點型最大值:                  checkDoubleMaxValue(str,val)
校正浮點型是否為非負數:             isNotNegativeDouble(str)
校正字串是否為日期型:             checkIsValidDate(str)
校正兩個日期的先後:                checkDateEarlier(strStart,strEnd)
校正字串是否為email型:           checkEmail(str)

校正字串是否為中文:               checkIsChinese(str)
計算字串的長度,一個漢字兩個字元:   realLength()
校正字串是否符合自訂Regex:   checkMask(str,pat)
得到檔案的尾碼名:                   getFilePostfix(oFile) 
-------------- 函數檢索 --------------
*/

/**
* added by LxcJie 2004.6.25
* 去除多餘空格函數
* trim:去除兩邊空格 lTrim:去除左空格 rTrim: 去除右空格
* 用法:
*     var str = "  hello ";
*     str = str.trim();
*/
String.prototype.trim = function()
{
    return this.replace(/(^[\\s]*)|([\\s]*$)/g, "");
}
String.prototype.lTrim = function()
{
    return this.replace(/(^[\\s]*)/g, "");
}
String.prototype.rTrim = function()
{
    return this.replace(/([\\s]*$)/g, "");
}
/********************************** Empty **************************************/
/**
*校正字串是否為空白
*傳回值:
*如果不為空白,定義校正通過,返回true
*如果為空白,校正不通過,返回false               參考提示資訊:輸入欄位不可為空!
*/
function checkIsNotEmpty(str)
{
    if(str.trim() == "")
        return false;
    else
        return true;
}//~~~
/*--------------------------------- Empty --------------------------------------*/
/********************************** Integer *************************************/
/**
*校正字串是否為整型
*傳回值:
*如果為空白,定義校正通過,      返回true
*如果字串全部為數字,校正通過,返回true
*如果校正不通過,              返回false     參考提示資訊:輸入欄位必須為數字!
*/
function checkIsInteger(str)
{
    //如果為空白,則通過校正
    if(str == "")
        return true;
    if(/^(\\-?)(\\d+)$/.test(str))
        return true;
    else
        return false;
}//~~~
/**
*校正整型最小值
*str:要校正的串。  val:比較的值
*
*傳回值:
*如果為空白,定義校正通過,                返回true
*如果滿足條件,大於等於給定值,校正通過,返回true
*如果小於給定值,                        返回false              參考提示資訊:輸入欄位不能小於給定值!
*/
function checkIntegerMinValue(str,val)
{
    //如果為空白,則通過校正
    if(str == "")
        return true;
    if(typeof(val) != "string")
        val = val + "";
    if(checkIsInteger(str) == true)
    {
        if(parseInt(str,10)>=parseInt(val,10))
            return true;
        else
            return false;
    }
    else
        return false;
}//~~~
/**
*校正整型最大值
*str:要校正的串。  val:比較的值
*
*傳回值:
*如果為空白,定義校正通過,                返回true
*如果滿足條件,小於等於給定值,校正通過,返回true
*如果大於給定值,                        返回false       參考提示資訊:輸入值不能大於給定值!
*/
function checkIntegerMaxValue(str,val)
{
    //如果為空白,則通過校正
    if(str == "")
        return true;
    if(typeof(val) != "string")
        val = val + "";
    if(checkIsInteger(str) == true)
    {
        if(parseInt(str,10)<=parseInt(val,10))
            return true;
        else
            return false;
    }
    else
        return false;
}//~~~
/**
*校正整型是否為非負數
*str:要校正的串。
*
*傳回值:
*如果為空白,定義校正通過,返回true
*如果非負數,            返回true
*如果是負數,            返回false               參考提示資訊:輸入值不能是負數!
*/
function isNotNegativeInteger(str)
{
    //如果為空白,則通過校正
    if(str == "")
        return true;
    if(checkIsInteger(str) == true)
    {
        if(parseInt(str,10) < 0)
            return false;
        else
            return true;
    }
    else
        return false;
}//~~~
/*--------------------------------- Integer --------------------------------------*/
/********************************** Double ****************************************/
/**
*校正字串是否為浮點型
*傳回值:
*如果為空白,定義校正通過,      返回true
*如果字串為浮點型,校正通過,  返回true
*如果校正不通過,              返回false     參考提示資訊:輸入欄位不是合法的浮點數!
*/
function checkIsDouble(str)
{
    //如果為空白,則通過校正
    if(str == "")
        return true;
    //如果是整數,則校正整數的有效性
    if(str.indexOf(".") == -1)
    {
        if(checkIsInteger(str) == true)
            return true;
        else
            return false;
    }
    else
    {
        if(/^(\\-?)(\\d+)(.{1})(\\d+)$/g.test(str))
            return true;
        else
            return false;
    }
}//~~~
/**
*校正浮點型最小值
*str:要校正的串。  val:比較的值
*
*傳回值:
*如果為空白,定義校正通過,                返回true
*如果滿足條件,大於等於給定值,校正通過,返回true
*如果小於給定值,                        返回false              參考提示資訊:輸入欄位不能小於給定值!
*/
function checkDoubleMinValue(str,val)
{
    //如果為空白,則通過校正
    if(str == "")
        return true;
    if(typeof(val) != "string")
        val = val + "";
    if(checkIsDouble(str) == true)
    {
        if(parseFloat(str)>=parseFloat(val))
            return true;
        else
            return false;
    }
    else
        return false;
}//~~~
/**
*校正浮點型最大值
*str:要校正的串。  val:比較的值
*
*傳回值:
*如果為空白,定義校正通過,                返回true
*如果滿足條件,小於等於給定值,校正通過,返回true
*如果大於給定值,                        返回false       參考提示資訊:輸入值不能大於給定值!
*/
function checkDoubleMaxValue(str,val)
{
    //如果為空白,則通過校正
    if(str == "")
        return true;
    if(typeof(val) != "string")
        val = val + "";
    if(checkIsDouble(str) == true)
    {
        if(parseFloat(str)<=parseFloat(val))
            return true;
        else
            return false;
    }
    else
        return false;
}//~~~
/**
*校正浮點型是否為非負數
*str:要校正的串。
*
*傳回值:
*如果為空白,定義校正通過,返回true
*如果非負數,            返回true
*如果是負數,            返回false               參考提示資訊:輸入值不能是負數!
*/
function isNotNegativeDouble(str)
{
    //如果為空白,則通過校正
    if(str == "")
        return true;
    if(checkIsDouble(str) == true)
    {
        if(parseFloat(str) < 0)
            return false;
        else
            return true;
    }
    else
        return false;
}//~~~
/*--------------------------------- Double ---------------------------------------*/
/********************************** date ******************************************/
/**
*校正字串是否為日期型
*傳回值:
*如果為空白,定義校正通過,           返回true
*如果字串為日期型,校正通過,       返回true
*如果日期不合法,                   返回false    參考提示資訊:輸入欄位的時間不合法!(yyyy-MM-dd)
*/
function checkIsValidDate(str)
{
    //如果為空白,則通過校正
    if(str == "")
        return true;
    var pattern = /^((\\d{4})|(\\d{2}))-(\\d{1,2})-(\\d{1,2})$/g;
    if(!pattern.test(str))
        return false;
    var arrDate = str.split("-");
    if(parseInt(arrDate[0],10) < 100)
        arrDate[0] = 2000 + parseInt(arrDate[0],10) + "";
    var date =  new Date(arrDate[0],(parseInt(arrDate[1],10) -1)+"",arrDate[2]);
    if(date.getYear() == arrDate[0]
       && date.getMonth() == (parseInt(arrDate[1],10) -1)+""
       && date.getDate() == arrDate[2])
        return true;
    else
        return false;
}//~~~
/**
*校正兩個日期的先後
*傳回值:
*如果其中有一個日期為空白,校正通過,          返回true
*如果起始日期早於等於終止日期,校正通過,   返回true
*如果起始日期晚於終止日期,                 返回false    參考提示資訊: 起始日期不能晚於結束日期。
*/
function checkDateEarlier(strStart,strEnd)
{
    if(checkIsValidDate(strStart) == false || checkIsValidDate(strEnd) == false)
        return false;
    //如果有一個輸入為空白,則通過檢驗
    if (( strStart == "" ) || ( strEnd == "" ))
        return true;
    var arr1 = strStart.split("-");
    var arr2 = strEnd.split("-");
    var date1 = new Date(arr1[0],parseInt(arr1[1].replace(/^0/,""),10) - 1,arr1[2]);
    var date2 = new Date(arr2[0],parseInt(arr2[1].replace(/^0/,""),10) - 1,arr2[2]);
    if(arr1[1].length == 1)
        arr1[1] = "0" + arr1[1];
    if(arr1[2].length == 1)
        arr1[2] = "0" + arr1[2];
    if(arr2[1].length == 1)
        arr2[1] = "0" + arr2[1];
    if(arr2[2].length == 1)
        arr2[2]="0" + arr2[2];
    var d1 = arr1[0] + arr1[1] + arr1[2];
    var d2 = arr2[0] + arr2[1] + arr2[2];
    if(parseInt(d1,10) > parseInt(d2,10))
       return false;
    else
       return true;
}//~~~
/*--------------------------------- date -----------------------------------------*/
/********************************** email *****************************************/
/**
*校正字串是否為email型
*傳回值:
*如果為空白,定義校正通過,           返回true
*如果字串為email型,校正通過,      返回true
*如果email不合法,                  返回false    參考提示資訊:Email的格式不正確!
*/
function checkEmail(str)
{
    //如果為空白,則通過校正
    if(str == "")
        return true;
    if (str.charAt(0) == "." || str.charAt(0) == "@" || str.indexOf(\'@\', 0) == -1
        || str.indexOf(\'.\', 0) == -1 || str.lastIndexOf("@") == str.length-1 || str.lastIndexOf(".") == str.length-1)
        return false;
    else
        return true;
}//~~~
/*--------------------------------- email ----------------------------------------*/
/********************************** chinese ***************************************/
/**
*校正字串是否為中文
*傳回值:
*如果為空白,定義校正通過,           返回true
*如果字串為中文,校正通過,         返回true
*如果字串為非中文,             返回false    參考提示資訊:必須為中文!
*/
function checkIsChinese(str)
{
    //如果值為空白,通過校正
    if (str == "")
        return true;
    var pattern = /^([\\u4E00-\\u9FA5]|[\\uFE30-\\uFFA0])*$/gi;
    if (pattern.test(str))
        return true;
    else
        return false;
}//~~~
/**
* 計算字串的長度,一個漢字兩個字元
*/
String.prototype.realLength = function()
{
  return this.replace(/[^\\x00-\\xff]/g,"**").length;
}
/*--------------------------------- chinese --------------------------------------*/
/********************************** mask ***************************************/
/**
*校正字串是否符合自訂Regex
*str 要校正的字串  pat 自訂的Regex
*傳回值:
*如果為空白,定義校正通過,           返回true
*如果字串符合,校正通過,           返回true
*如果字串不符合,                   返回false    參考提示資訊:必須滿足***模式
*/
function checkMask(str,pat)
{
    //如果值為空白,通過校正
    if (str == "")
        return true;
    var pattern = new RegExp(pat,"gi")
    if (pattern.test(str))
        return true;
    else
        return false;
}//~~~
/*--------------------------------- mask --------------------------------------*/
/********************************** file ***************************************/
/**
* added by LxcJie 2004.6.25
* 得到檔案的尾碼名
* oFile為file控制項對象
*/
function getFilePostfix(oFile)
{
    if(oFile == null)
        return null;
    var pattern = /(.*)\\.(.*)$/gi;
    if(typeof(oFile) == "object")
    {
        if(oFile.value == null || oFile.value == "")
            return null;
        var arr = pattern.exec(oFile.value);
        return RegExp.$2;
    }
    else if(typeof(oFile) == "string")
    {
        var arr = pattern.exec(oFile);
        return RegExp.$2;
    }
    else
        return null;
}//~~~
/*--------------------------------- file --------------------------------------*/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.