`

常用JS函数

阅读更多

常用javascript函数(一) 
作者: 无名 类别: HTML/JavaScript 日期: 2001-11-11 21:19:11 
 
/*********************************************************************************
* EO_JSLib.js
* javascript正则表达式检验
**********************************************************************************/


//校验是否全由数字组成
function isDigit(s)
{
var patrn=/^[0-9]{1,20}$/;
if (!patrn.exec(s)) return false
return true
}

//校验登录名:只能输入5-20个以字母开头、可带数字、“_”、“.”的字串
function isRegisterUserName(s)
{
var patrn=/^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$/;
if (!patrn.exec(s)) return false
return true
}

//校验用户姓名:只能输入1-30个以字母开头的字串
function isTrueName(s)
{
var patrn=/^[a-zA-Z]{1,30}$/;
if (!patrn.exec(s)) return false
return true
}

//校验密码:只能输入6-20个字母、数字、下划线
function isPasswd(s)
{
var patrn=/^(\w){6,20}$/;
if (!patrn.exec(s)) return false
return true
}

//校验普通电话、传真号码:可以“+”开头,除数字外,可含有“-”
function isTel(s)
{
//var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?(\d){1,12})+$/;
var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;
if (!patrn.exec(s)) return false
return true
}

//校验手机号码:必须以数字开头,除数字外,可含有“-”
function isMobil(s)
{
var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;
if (!patrn.exec(s)) return false
return true
}

//校验邮政编码
function isPostalCode(s)
{
//var patrn=/^[a-zA-Z0-9]{3,12}$/;
var patrn=/^[a-zA-Z0-9 ]{3,12}$/;
if (!patrn.exec(s)) return false
return true
}

//校验搜索关键字
function isSearch(s)
{
var patrn=/^[^`~!@#$%^&*()+=|\\\][\]\{\}:;\'\,.<>/?]{1}[^`~!@$%^&()+=|\\\][\]\{\}:;\'\,.<>?]{0,19}$/;
if (!patrn.exec(s)) return false
return true
}

function isIP(s) //by zergling
{
var patrn=/^[0-9.]{1,20}$/;
if (!patrn.exec(s)) return false
return true
}
 
 
/*********************************************************************************
* FUNCTION: isBetween
* PARAMETERS: val AS any value
* lo AS Lower limit to check
* hi AS Higher limit to check
* CALLS: NOTHING
* RETURNS: TRUE if val is between lo and hi both inclusive, otherwise false.
**********************************************************************************/
function isBetween (val, lo, hi) {
if ((val < lo) || (val > hi)) { return(false); }
else { return(true); }
}

/*********************************************************************************
* FUNCTION: isDate checks a valid date
* PARAMETERS: theStr AS String
* CALLS: isBetween, isInt
* RETURNS: TRUE if theStr is a valid date otherwise false.
**********************************************************************************/
function isDate (theStr) {
var the1st = theStr.indexOf('-');
var the2nd = theStr.lastIndexOf('-');

if (the1st == the2nd) { return(false); }
else {
var y = theStr.substring(0,the1st);
var m = theStr.substring(the1st+1,the2nd);
var d = theStr.substring(the2nd+1,theStr.length);
var maxDays = 31;

if (isInt(m)==false || isInt(d)==false || isInt(y)==false) {
return(false); }
else if (y.length < 4) { return(false); }
else if (!isBetween (m, 1, 12)) { return(false); }
else if (m==4 || m==6 || m==9 || m==11) maxDays = 30;
else if (m==2) {
if (y % 4 > 0) maxDays = 28;
else if (y % 100 == 0 && y % 400 > 0) maxDays = 28;
else maxDays = 29;
}
if (isBetween(d, 1, maxDays) == false) { return(false); }
else { return(true); }
}
}
/*********************************************************************************
* FUNCTION: isEuDate checks a valid date in British format
* PARAMETERS: theStr AS String
* CALLS: isBetween, isInt
* RETURNS: TRUE if theStr is a valid date otherwise false.
**********************************************************************************/
function isEuDate (theStr) {
if (isBetween(theStr.length, 8, 10) == false) { return(false); }
else {
var the1st = theStr.indexOf('/');
var the2nd = theStr.lastIndexOf('/');

if (the1st == the2nd) { return(false); }
else {
var m = theStr.substring(the1st+1,the2nd);
var d = theStr.substring(0,the1st);
var y = theStr.substring(the2nd+1,theStr.length);
var maxDays = 31;

if (isInt(m)==false || isInt(d)==false || isInt(y)==false) {
return(false); }
else if (y.length < 4) { return(false); }
else if (isBetween (m, 1, 12) == false) { return(false); }
else if (m==4 || m==6 || m==9 || m==11) maxDays = 30;
else if (m==2) {
if (y % 4 > 0) maxDays = 28;
else if (y % 100 == 0 && y % 400 > 0) maxDays = 28;
else maxDays = 29;
}

if (isBetween(d, 1, maxDays) == false) { return(false); }
else { return(true); }
}
}

}
/********************************************************************************
* FUNCTION: Compare Date! Which is the latest!
* PARAMETERS: lessDate,moreDate AS String
* CALLS: isDate,isBetween
* RETURNS: TRUE if lessDate<moreDate
*********************************************************************************/
function isComdate (lessDate , moreDate)
{
if (!isDate(lessDate)) { return(false);}
if (!isDate(moreDate)) { return(false);}
var less1st = lessDate.indexOf('-');
var less2nd = lessDate.lastIndexOf('-');
var more1st = moreDate.indexOf('-');
var more2nd = moreDate.lastIndexOf('-');
var lessy = lessDate.substring(0,less1st);
var lessm = lessDate.substring(less1st+1,less2nd);
var lessd = lessDate.substring(less2nd+1,lessDate.length);
var morey = moreDate.substring(0,more1st);
var morem = moreDate.substring(more1st+1,more2nd);
var mored = moreDate.substring(more2nd+1,moreDate.length);
var Date1 = new Date(lessy,lessm,lessd);
var Date2 = new Date(morey,morem,mored);
if (Date1>Date2) { return(false);}
return(true);

}

/*********************************************************************************
* FUNCTION isEmpty checks if the parameter is empty or null
* PARAMETER str AS String
**********************************************************************************/
function isEmpty (str) {
if ((str==null)||(str.length==0)) return true;
else return(false);
}

/*********************************************************************************
* FUNCTION: isInt
* PARAMETER: theStr AS String
* RETURNS: TRUE if the passed parameter is an integer, otherwise FALSE
* CALLS: isDigit
**********************************************************************************/
function isInt (theStr) {
var flag = true;

if (isEmpty(theStr)) { flag=false; }
else
{ for (var i=0; i<theStr.length; i++) {
if (isDigit(theStr.substring(i,i+1)) == false) {
flag = false; break;
}
}
}
return(flag);
}

/*********************************************************************************
* FUNCTION: isReal
* PARAMETER: heStr AS String
decLen AS Integer (how many digits after period)
* RETURNS: TRUE if theStr is a float, otherwise FALSE
* CALLS: isInt
**********************************************************************************/
function isReal (theStr, decLen) {
var dot1st = theStr.indexOf('.');
var dot2nd = theStr.lastIndexOf('.');
var OK = true;

if (isEmpty(theStr)) return false;

if (dot1st == -1) {
if (!isInt(theStr)) return(false);
else return(true);
}

else if (dot1st != dot2nd) return (false);
else if (dot1st==0) return (false);
else {
var intPart = theStr.substring(0, dot1st);
var decPart = theStr.substring(dot2nd+1);

if (decPart.length > decLen) return(false);
else if (!isInt(intPart) || !isInt(decPart)) return (false);
else if (isEmpty(decPart)) return (false);
else return(true);
}
}

/*********************************************************************************
* FUNCTION: isEmail
* PARAMETER: String (Email Address)
* RETURNS: TRUE if the String is a valid Email address
* FALSE if the passed string is not a valid Email Address
* EMAIL FORMAT: AnyName@EmailServer e.g; webmaster@hotmail.com
* @ sign can appear only once in the email address.
*********************************************************************************/
function isEmail (theStr) {
var atIndex = theStr.indexOf('@');
var dotIndex = theStr.indexOf('.', atIndex);
var flag = true;
theSub = theStr.substring(0, dotIndex+1)

if ((atIndex < 1)||(atIndex != theStr.lastIndexOf('@'))||(dotIndex < atIndex + 2)||(theStr.length <= theSub.length))
{ return(false); }
else { return(true); }
}
/*********************************************************************************
* FUNCTION: newWindow
* PARAMETERS: doc -> Document to open in the new window
hite -> Height of the new window
wide -> Width of the new window
bars -> 1-Scroll bars = YES 0-Scroll Bars = NO
resize -> 1-Resizable = YES 0-Resizable = NO
* CALLS: NONE
* RETURNS: New window instance
**********************************************************************************/
function newWindow (doc, hite, wide, bars, resize) {
var winNew="_blank";
var opt="toolbar=0,location=0,directories=0,status=0,menubar=0,";
opt+=("scrollbars="+bars+",");
opt+=("resizable="+resize+",");
opt+=("width="+wide+",");
opt+=("height="+hite);
winHandle=window.open(doc,winNew,opt);
return;
}
/*********************************************************************************
* FUNCTION: DecimalFormat
* PARAMETERS: paramValue -> Field value
* CALLS: NONE
* RETURNS: Formated string
**********************************************************************************/
function DecimalFormat (paramValue) {
var intPart = parseInt(paramValue);
var decPart =parseFloat(paramValue) - intPart;

str = "";
if ((decPart == 0) || (decPart == null)) str += (intPart + ".00");
else str += (intPart + decPart);

return (str);
}

 

"^\\d+$"  //非负整数(正整数 + 0)
"^[0-9]*[1-9][0-9]*$"  //正整数
"^((-\\d+)|(0+))$"  //非正整数(负整数 + 0)
"^-[0-9]*[1-9][0-9]*$"  //负整数
"^-?\\d+$"    //整数
"^\\d+(\\.\\d+)?$"  //非负浮点数(正浮点数 + 0)
"^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$"  //正浮点数
"^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$"  //非正浮点数(负浮点数 + 0)
"^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"  //负浮点数
"^(-?\\d+)(\\.\\d+)?$"  //浮点数
"^[A-Za-z]+$"  //由26个英文字母组成的字符串
"^[A-Z]+$"  //由26个英文字母的大写组成的字符串
"^[a-z]+$"  //由26个英文字母的小写组成的字符串
"^[A-Za-z0-9]+$"  //由数字和26个英文字母组成的字符串
"^\\w+$"  //由数字、26个英文字母或者下划线组成的字符串
"^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$"    //email地址
"^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$"  //url

分享到:
评论

相关推荐

    常用js函数

    常用js函数

    asp.net开发常用javascript函数封装(c#)

    本文将详细讲解如何在C#中封装常用的JavaScript函数,并探讨这些函数在实际开发中的应用。 一、JavaScript函数封装的意义 在ASP.NET项目中,为了提高代码复用性,降低维护成本,我们常常会将一些常用的功能封装成...

    常用 JS 函数.docx

    常用 JS 函数手册 本文档涵盖了多种常用的 JS 函数,旨在提高开发效率。这些函数涵盖了时间格式化、URL 参数获取、手机端浏览器判断、数组降维等多个方面。 时间格式化 时间格式化是开发中非常常用的功能,JS ...

    javascript常用函数 javascript 常用库

    一、JavaScript常用函数 1. 数组操作函数 - `push()`: 向数组末尾添加一个或多个元素,并返回新长度。 - `pop()`: 删除并返回数组最后一个元素。 - `shift()`: 删除并返回数组第一个元素。 - `unshift()`: 在...

    基础常用JS函数和语法集合大全

    基础常用JS函数和语法集合大全 本文总结了 JavaScript 中常用的函数和语法,涵盖了输出语句、注释、HTML 文档结构、DOM 顺序、元素选择、字符串操作、值类型转换、比较操作符、变量声明、判断语句、循环结构、函数...

    常用JavaScript函数 16 - 30(自我总结)

    这篇自我总结涵盖了JavaScript函数的16到30个常用知识点,旨在帮助开发者更好地理解和应用这些功能强大的工具。 1. **数组操作** - `slice()`:用于从原数组中提取一个子数组并返回,不会改变原数组。 - `splice...

    几个常用javascript函数

    本篇文章将深入探讨几个常用的JavaScript函数,这些函数在实际开发中非常常见且实用。 1. `console.log()` 这是开发者调试代码时最常用的函数之一。它用于在浏览器的控制台输出信息,帮助开发者查看程序执行过程中...

    100多个基础常用JS函数和各种数据转换处理集合大全.zip

    这个名为"100多个基础常用JS函数和各种数据转换处理集合大全.zip"的压缩包文件,显然是一个包含了大量实用JS函数和数据处理方法的资源库,对于学习和提升JavaScript编程技能非常有帮助。 该资源库可能是基于`outils...

    常用js函数八枚

    常用js函数八枚,对于入门者很有帮助. 添加、去除事件处理函数的方法/去除事件监听者:detachEventListener()/检查浏览器/根据特定属性值来选择元素 等等

    常用javascript函数

    常用Jquery语句 常用javascript函数 可以直接使用

    常用 JS 函数.pdf

    这篇文档主要介绍了几个常用且实用的JS函数,可以帮助开发者更高效地处理时间格式、获取指定时间戳以及解析URL参数。 1. **时间格式化**:`formatDate`函数用于将日期对象或时间戳转换为指定格式的字符串。它接受两...

    常用表单js函数js单

    常用表单js函数,一些很常用的函数,方便验证

    javascript常用函数集合

    这个"javascript常用函数集合"涵盖了JavaScript编程中常见的函数和概念,是学习和提升JavaScript技能的重要资源。以下是对这些常见函数和基础知识的详细解释: 1. **变量声明**:在JavaScript中,我们可以使用`var`...

    javascript常用方法函数收集

    web前端javascript常用的的公共处理函数大全,性能较好。

    JS常用函数(笔记)

    以下是一些JS常用函数的详细说明: 1. **事件处理**: - `click()`: 该方法用于模拟用户对元素的点击操作,通常用于按钮或链接等交互元素。 - `closed`: 这是一个属性,用于检查窗口是否已关闭,返回`true`或`...

    js常用函数大全

    根据给定的信息,我们可以整理出JavaScript中常用的几大类函数:常规函数、数组方法、日期对象方法以及字符串处理方法。 ### 一、常规函数 #### 1. Alert `alert()`函数用于显示一个带有消息和确定按钮的警告框。 ...

    JavaScript常用函数练习

    JavaScript学习函数时,常常会听懂了却做不来实际例子,本资源有部分函数的经典案例及答案,其中有些是基础题,有些逻辑对新人来说难以理解,希望对大家有所帮助。

    JavaScript学习笔记_js常用函数封装_js包.zip

    本压缩包“JavaScript学习笔记_js常用函数封装_js包.zip”包含了对JavaScript基础及进阶技巧的学习资料,特别关注了函数封装和模块化开发实践。 首先,`tool.js`可能是一个实用工具函数集合,封装了一些常见的...

    自己封装的常用javascript函数分享

    标题《自己封装的常用javascript函数分享》中提到的知识点主要包括以下几个方面: 1. Cookie操作函数封装 - `setCookie(name, value, iDay)`:设置cookie的值,可自定义有效期天数。如果`iDay`参数为`false`,则...

Global site tag (gtag.js) - Google Analytics