`
iwebcode
  • 浏览: 2011153 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

Javascript函数大全

 
阅读更多

转载:http://www.cnblogs.com/zhuboxingzbx/archive/2007/12/20/1007857.html

/*
--------------函数检索--------------
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)
--------------函数检索--------------
*/

--------------函数检索--------------
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)
--------------函数检索--------------
*/

/**
*addedbyLxcJie2004.6.25
*去除多余空格函数
*trim:去除两边空格lTrim:去除左空格rTrim:去除右空格
*用法:
*varstr="hello";
*str=str.trim();
*/
String.prototype.trim=function()
{
returnthis.replace(/(^[\s]*)|([\s]*$)/g,"");
}
String.prototype.lTrim=function()
{
returnthis.replace(/(^[\s]*)/g,"");
}
String.prototype.rTrim=function()
{
returnthis.replace(/([\s]*$)/g,"");
}
/**********************************Empty**************************************/
/**
*校验字符串是否为空
*返回值:
*如果不为空,定义校验通过,返回true
*如果为空,校验不通过,返回false参考提示信息:输入域不能为空!
*/
functioncheckIsNotEmpty(str)
{
if(str.trim()=="")
returnfalse;
else
returntrue;
}//~~~
/*---------------------------------Empty--------------------------------------*/
/**********************************Integer*************************************/
/**
*校验字符串是否为整型
*返回值:
*如果为空,定义校验通过,返回true
*如果字串全部为数字,校验通过,返回true
*如果校验不通过,返回false参考提示信息:输入域必须为数字!
*/
functioncheckIsInteger(str)
{
//如果为空,则通过校验
if(str=="")
returntrue;
if(/^(\-?)(\d+)$/.test(str))
returntrue;
else
returnfalse;
}//~~~
/**
*校验整型最小值
*str:要校验的串。val:比较的值
*
*返回值:
*如果为空,定义校验通过,返回true
*如果满足条件,大于等于给定值,校验通过,返回true
*如果小于给定值,返回false参考提示信息:输入域不能小于给定值!
*/
functioncheckIntegerMinValue(str,val)
{
//如果为空,则通过校验
if(str=="")
returntrue;
if(typeof(val)!="string")
val=val+"";
if(checkIsInteger(str)==true)
{
if(parseInt(str,10)>=parseInt(val,10))
returntrue;
else
returnfalse;
}
else
returnfalse;
}//~~~
/**
*校验整型最大值
*str:要校验的串。val:比较的值
*
*返回值:
*如果为空,定义校验通过,返回true
*如果满足条件,小于等于给定值,校验通过,返回true
*如果大于给定值,返回false参考提示信息:输入值不能大于给定值!
*/
functioncheckIntegerMaxValue(str,val)
{
//如果为空,则通过校验
if(str=="")
returntrue;
if(typeof(val)!="string")
val=val+"";
if(checkIsInteger(str)==true)
{
if(parseInt(str,10)<=parseInt(val,10))
returntrue;
else
returnfalse;
}
else
returnfalse;
}//~~~
/**
*校验整型是否为非负数
*str:要校验的串。
*
*返回值:
*如果为空,定义校验通过,返回true
*如果非负数,返回true
*如果是负数,返回false参考提示信息:输入值不能是负数!
*/
functionisNotNegativeInteger(str)
{
//如果为空,则通过校验
if(str=="")
returntrue;
if(checkIsInteger(str)==true)
{
if(parseInt(str,10)<0)
returnfalse;
else
returntrue;
}
else
returnfalse;
}//~~~
/*---------------------------------Integer--------------------------------------*/
/**********************************Double****************************************/
/**
*校验字符串是否为浮点型
*返回值:
*如果为空,定义校验通过,返回true
*如果字串为浮点型,校验通过,返回true
*如果校验不通过,返回false参考提示信息:输入域不是合法的浮点数!
*/
functioncheckIsDouble(str)
{
//如果为空,则通过校验
if(str=="")
returntrue;
//如果是整数,则校验整数的有效性
if(str.indexOf(".")==-1)
{
if(checkIsInteger(str)==true)
returntrue;
else
returnfalse;
}
else
{
if(/^(\-?)(\d+)(.{1})(\d+)$/g.test(str))
returntrue;
else
returnfalse;
}
}//~~~
/**
*校验浮点型最小值
*str:要校验的串。val:比较的值
*
*返回值:
*如果为空,定义校验通过,返回true
*如果满足条件,大于等于给定值,校验通过,返回true
*如果小于给定值,返回false参考提示信息:输入域不能小于给定值!
*/
functioncheckDoubleMinValue(str,val)
{
//如果为空,则通过校验
if(str=="")
returntrue;
if(typeof(val)!="string")
val=val+"";
if(checkIsDouble(str)==true)
{
if(parseFloat(str)>=parseFloat(val))
returntrue;
else
returnfalse;
}
else
returnfalse;
}//~~~
/**
*校验浮点型最大值
*str:要校验的串。val:比较的值
*
*返回值:
*如果为空,定义校验通过,返回true
*如果满足条件,小于等于给定值,校验通过,返回true
*如果大于给定值,返回false参考提示信息:输入值不能大于给定值!
*/
functioncheckDoubleMaxValue(str,val)
{
//如果为空,则通过校验
if(str=="")
returntrue;
if(typeof(val)!="string")
val=val+"";
if(checkIsDouble(str)==true)
{
if(parseFloat(str)<=parseFloat(val))
returntrue;
else
returnfalse;
}
else
returnfalse;
}//~~~
/**
*校验浮点型是否为非负数
*str:要校验的串。
*
*返回值:
*如果为空,定义校验通过,返回true
*如果非负数,返回true
*如果是负数,返回false参考提示信息:输入值不能是负数!
*/
functionisNotNegativeDouble(str)
{
//如果为空,则通过校验
if(str=="")
returntrue;
if(checkIsDouble(str)==true)
{
if(parseFloat(str)<0)
returnfalse;
else
returntrue;
}
else
returnfalse;
}//~~~
/*---------------------------------Double---------------------------------------*/
/**********************************date******************************************/
/**
*校验字符串是否为日期型
*返回值:
*如果为空,定义校验通过,返回true
*如果字串为日期型,校验通过,返回true
*如果日期不合法,返回false参考提示信息:输入域的时间不合法!(yyyy-MM-dd)
*/
functioncheckIsValidDate(str)
{
//如果为空,则通过校验
if(str=="")
returntrue;
varpattern=/^((\d{4})|(\d{2}))-(\d{1,2})-(\d{1,2})$/g;
if(!pattern.test(str))
returnfalse;
vararrDate=str.split("-");
if(parseInt(arrDate[0],10)<100)
arrDate[0]=2000+parseInt(arrDate[0],10)+"";
vardate=newDate(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])
returntrue;
else
returnfalse;
}//~~~
/**
*校验两个日期的先后
*返回值:
*如果其中有一个日期为空,校验通过,返回true
*如果起始日期早于等于终止日期,校验通过,返回true
*如果起始日期晚于终止日期,返回false参考提示信息:起始日期不能晚于结束日期。
*/
functioncheckDateEarlier(strStart,strEnd)
{
if(checkIsValidDate(strStart)==false||checkIsValidDate(strEnd)==false)
returnfalse;
//如果有一个输入为空,则通过检验
if((strStart=="")||(strEnd==""))
returntrue;
vararr1=strStart.split("-");
vararr2=strEnd.split("-");
vardate1=newDate(arr1[0],parseInt(arr1[1].replace(/^0/,""),10)-1,arr1[2]);
vardate2=newDate(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];
vard1=arr1[0]+arr1[1]+arr1[2];
vard2=arr2[0]+arr2[1]+arr2[2];
if(parseInt(d1,10)>parseInt(d2,10))
returnfalse;
else
returntrue;
}//~~~
/*---------------------------------date-----------------------------------------*/
/**********************************email*****************************************/
/**
*校验字符串是否为email型
*返回值:
*如果为空,定义校验通过,返回true
*如果字串为email型,校验通过,返回true
*如果email不合法,返回false参考提示信息:Email的格式不正確!
*/
functioncheckEmail(str)
{
//如果为空,则通过校验
if(str=="")
returntrue;
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)
returnfalse;
else
returntrue;
}//~~~
/*---------------------------------email----------------------------------------*/
/**********************************chinese***************************************/
/**
*校验字符串是否为中文
*返回值:
*如果为空,定义校验通过,返回true
*如果字串为中文,校验通过,返回true
*如果字串为非中文,返回false参考提示信息:必须为中文!
*/
functioncheckIsChinese(str)
{
//如果值为空,通过校验
if(str=="")
returntrue;
varpattern=/^([\u4E00-\u9FA5]|[\uFE30-\uFFA0])*$/gi;
if(pattern.test(str))
returntrue;
else
returnfalse;
}//~~~
/**
*计算字符串的长度,一个汉字两个字符
*/
String.prototype.realLength=function()
{
returnthis.replace(/[^\x00-\xff]/g,"**").length;
}
/*---------------------------------chinese--------------------------------------*/
/**********************************mask***************************************/
/**
*校验字符串是否符合自定义正则表达式
*str要校验的字串pat自定义的正则表达式
*返回值:
*如果为空,定义校验通过,返回true
*如果字串符合,校验通过,返回true
*如果字串不符合,返回false参考提示信息:必须满足***模式
*/
functioncheckMask(str,pat)
{
//如果值为空,通过校验
if(str=="")
returntrue;
varpattern=newRegExp(pat,"gi")
if(pattern.test(str))
returntrue;
else
returnfalse;
}//~~~
/*---------------------------------mask--------------------------------------*/
/**********************************file***************************************/
/**
*addedbyLxcJie2004.6.25
*得到文件的后缀名
*oFile为file控件对象
*/
functiongetFilePostfix(oFile)
{
if(oFile==null)
returnnull;
varpattern=/(.*)\.(.*)$/gi;
if(typeof(oFile)=="object")
{
if(oFile.value==null||oFile.value=="")
returnnull;
vararr=pattern.exec(oFile.value);
returnRegExp.$2;
}
elseif(typeof(oFile)=="string")
{
vararr=pattern.exec(oFile);
returnRegExp.$2;
}
else
returnnull;
}//~~~
/*---------------------------------file--------------------------------------*/

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics