网络编程
位置:首页>> 网络编程>> JavaScript>> 17个javascript自定义函数库

17个javascript自定义函数库

 来源:asp之家 发布时间:2008-02-26 11:49:00 

标签:函数,javascript,库

-------------- 函数检索 --------------
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()
校验字符串是否符合自定义正则表达式: 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;
}//~~~

 

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com