javascript函数库

2016-02-19 13:26 1 1 收藏

今天天气好晴朗处处好风光,好天气好开始,图老师又来和大家分享啦。下面给大家推荐javascript函数库,希望大家看完后也有个好心情,快快行动吧!

【 tulaoshi.com - Web开发 】

/*

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

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

/**
* 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
(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)sp; 参考提示信息:输入值不能大于给定值!
*/
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
      &nbs

p;  }
    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;
}//~~~
/**
*校验两个日期的先后
*返回值:
*如果其中有一个日期为空,校验通过,          返回tru

sp;   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 ***************************************/
/**
*校验字符串是否符合自定义正则表达式
*str 要校验的字串  pat 自定义的正则表达式
*返回值:
*如果为空,定义校验通过,           返回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 == "")
           

>*如果为空,校验不通过,返回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     &nb

来源:https://www.tulaoshi.com/n/20160219/1604283.html

延伸阅读
标签: PHP
  <?php // // SourceForge: Breaking Down the Barriers to Open Source Development // Copyright 1999-2000 (c) The SourceForge Crew // http://sourceforge.net // // $Id: database.php,v 1.6 2000/04/11 14:17:13 cvs Exp $ // // /etc/local.inc includes the machine specific database connect info function db_c...
标签: Web开发
?php$hidden_hash_var='your_password_here';$LOGGED_IN=false;//clear it out in case someone sets it in the URL or somethingunset($LOGGED_IN);/*create table user (user_id int not null auto_increment primary key,user_name text,real_name text,email text,password text,remote_addr text,confirm_hash text,is_conf...
在VC中使用MATLAB C/C++函数库 作者: 殷延伟 下载示例代码1 下载示例代码2 MATLAB广泛应用于线性代数、自动控制理论、数理统计、数字信号处理、时间序列分析、动态系统仿真等领域。因此如果在VC中对MATLAB进行调用将大大减少编程的工作量、保证程序的准确性,并且继承了VC++强大的功能,提高...
标签: Web开发
在PHP中有两套正则表达式函数库,两者功能相似,只是执行效率略有差异: 一套是由PCRE(Perl Compatible Regular Expression)库提供的。使用“preg_”为前缀命名的函数; 一套由POSIX(Portable Operating System Interface of Unix )扩展提供的(PHP默认)。使用以“ereg_”为前缀命名的函数; PHP中,正则表达式有三个作用: 匹配,也常...
标签: Web开发
函数为程序设计人员提供了一个非常方便的能力。通常在进行一个复杂的程序设计时,总是根据所要完成的功能,将程序划分为一些相对独立的部分,每部分编写一个函数。从而,使各部分充分独立,任务单一,程序清晰,易懂、易读、易维护。JavaScript函数可以封装那些在程序中可能要多次用到的模块。并可作为事件驱动的结果而调用的程序。从而实现...

经验教程

127

收藏

35
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部