/*
-------------- 函數(shù)檢索 --------------
trim函數(shù):???????????????????????? trim() lTrim() rTrim()
校驗字符串是否為空:???????????????? checkIsNotEmpty(str)
校驗字符串是否為整型:?????????????? checkIsInteger(str)
校驗整型最小值:??????????????????? checkIntegerMinValue(str,val)
校驗整型最大值:??????????????????? checkIntegerMaxValue(str,val)
校驗整型是否為非負(fù)數(shù):?????????????? isNotNegativeInteger(str)
校驗字符串是否為浮點型:???????????? checkIsDouble(str)
校驗浮點型最小值:????????????????? checkDoubleMinValue(str,val)
校驗浮點型最大值:????????????????? checkDoubleMaxValue(str,val)
校驗浮點型是否為非負(fù)數(shù):???????????? isNotNegativeDouble(str)
校驗字符串是否為日期型:???????????? checkIsValidDate(str)
校驗兩個日期的先后:??????????????? checkDateEarlier(strStart,strEnd)
校驗字符串是否為email型:?????????? checkEmail(str)
校驗字符串是否為中文:?????????????? checkIsChinese(str)
計算字符串的長度,一個漢字兩個字符:?? realLength()
校驗字符串是否符合自定義正則表達(dá)式:?? checkMask(str,pat)
得到文件的后綴名:?????????????????? getFilePostfix(oFile)?
-------------- 函數(shù)檢索 --------------
*/
/**
* added by LxcJie 2004.6.25
* 去除多余空格函數(shù)
* 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
*如果字串全部為數(shù)字,校驗通過,返回true
*如果校驗不通過,????????????? 返回false???? 參考提示信息:輸入域必須為數(shù)字!
*/
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;
}//~~~
/**
*校驗整型是否為非負(fù)數(shù)
*str:要校驗的串。
*
*返回值:
*如果為空,定義校驗通過,返回true
*如果非負(fù)數(shù),??????????? 返回true
*如果是負(fù)數(shù),??????????? 返回false?????????????? 參考提示信息:輸入值不能是負(fù)數(shù)!
*/
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???? 參考提示信息:輸入域不是合法的浮點數(shù)!
*/
function checkIsDouble(str)
{
??? //如果為空,則通過校驗
??? if(str == "")
??????? return true;
??? //如果是整數(shù),則校驗整數(shù)的有效性
??? 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;
}//~~~
/**
*校驗浮點型是否為非負(fù)數(shù)
*str:要校驗的串。
*
*返回值:
*如果為空,定義校驗通過,返回true
*如果非負(fù)數(shù),??????????? 返回true
*如果是負(fù)數(shù),??????????? 返回false?????????????? 參考提示信息:輸入值不能是負(fù)數(shù)!
*/
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??? 參考提示信息: 起始日期不能晚于結(jié)束日期。
*/
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 ***************************************/
/**
*校驗字符串是否符合自定義正則表達(dá)式
*str 要校驗的字串? pat 自定義的正則表達(dá)式
*返回值:
*如果為空,定義校驗通過,?????????? 返回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 --------------------------------------*/
posted on 2006-04-05 16:34
SIMONE 閱讀(231)
評論(0) 編輯 收藏 所屬分類:
JavaScript