常用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=/^[^~!@#$%^&amp;*()+=|\\\\\\][\\]\\{\\}:;\'\,.&lt;&gt;/?]{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
  1<moredate (!isdate(lessdate))="" (!isdate(moredate))="" (date1="" (lessdate="" *********************************************************************************="" ,="" date(lessy,lessm,lessd);="" date(morey,morem,mored);="" date1="new" date2="new" function="" if="" iscomdate="" less1st="lessDate.indexOf('-');" less2nd="lessDate.lastIndexOf('-');" lessd="lessDate.substring(less2nd+1,lessDate.length);" lessm="lessDate.substring(less1st+1,less2nd);" lessy="lessDate.substring(0,less1st);" more1st="moreDate.indexOf('-');" more2nd="moreDate.lastIndexOf('-');" mored="moreDate.substring(more2nd+1,moreDate.length);" moredate)="" morem="moreDate.substring(more1st+1,more2nd);" morey="moreDate.substring(0,more1st);" return(false);}="" var="" {="">Date2) { return(false);}   
  2return(true); 
  3
  4} 
  5
  6/*********************************************************************************   
  7* FUNCTION isEmpty checks if the parameter is empty or null   
  8* PARAMETER str AS String   
  9**********************************************************************************/   
 10function isEmpty (str) {   
 11if ((str==null)||(str.length==0)) return true;   
 12else return(false);   
 13} 
 14
 15/*********************************************************************************   
 16* FUNCTION: isInt   
 17* PARAMETER: theStr AS String   
 18* RETURNS: TRUE if the passed parameter is an integer, otherwise FALSE   
 19* CALLS: isDigit   
 20**********************************************************************************/   
 21function isInt (theStr) {   
 22var flag = true; 
 23
 24if (isEmpty(theStr)) { flag=false; }   
 25else   
 26{ for (var i=0; i<thestr.length; !="dot2nd)" (!isint(thestr))="" (decpart.length="" (dot1st="0)" (false);="" (how="" (isdigit(thestr.substring(i,i+1))="false)" (isempty(thestr))="" (thestr,="" *="" *********************************************************************************="" **********************************************************************************="" a="" after="" as="" break;="" calls:="" declen="" declen)="" decpart="theStr.substring(dot2nd+1);" digits="" dot1st="theStr.indexOf('.');" dot1st);="" dot2nd="theStr.lastIndexOf('.');" else="" false="" false;="" flag="false;" float,="" function="" function:="" hestr="" i++)="" if="" integer="" intpart="theStr.substring(0," is="" isint="" isreal="" many="" ok="true;" otherwise="" parameter:="" period)="" return="" return(false);="" return(flag);="" return(true);="" returns:="" string="" thestr="" true="" var="" {="" }=""> decLen) return(false);   
 27else if (!isInt(intPart) || !isInt(decPart)) return (false);   
 28else if (isEmpty(decPart)) return (false);   
 29else return(true);   
 30}   
 31} 
 32
 33/*********************************************************************************   
 34* FUNCTION: isEmail   
 35* PARAMETER: String (Email Address)   
 36* RETURNS: TRUE if the String is a valid Email address   
 37* FALSE if the passed string is not a valid Email Address   
 38* EMAIL FORMAT: AnyName@EmailServer e.g; [email protected]   
 39* @ sign can appear only once in the email address.   
 40*********************************************************************************/   
 41function isEmail (theStr) {   
 42var atIndex = theStr.indexOf( '@' );   
 43var dotIndex = theStr.indexOf('.', atIndex);   
 44var flag = true;   
 45theSub = theStr.substring(0, dotIndex+1) 
 46
 47if ((atIndex &lt; 1)||(atIndex != theStr.lastIndexOf( '@'))||(dotIndex &lt; atIndex + 2)||(theStr.length &lt;= theSub.length))   
 48{ return(false); }   
 49else { return(true); }   
 50}   
 51/*********************************************************************************   
 52* FUNCTION: newWindow   
 53* PARAMETERS: doc -&gt; Document to open in the new window   
 54hite -&gt; Height of the new window   
 55wide -&gt; Width of the new window   
 56bars -&gt; 1-Scroll bars = YES 0-Scroll Bars = NO   
 57resize -&gt; 1-Resizable = YES 0-Resizable = NO   
 58* CALLS: NONE   
 59* RETURNS: New window instance   
 60**********************************************************************************/   
 61function newWindow (doc, hite, wide, bars, resize) {   
 62var winNew="_blank";   
 63var opt="toolbar=0,location=0,directories=0,status=0,menubar=0,";   
 64opt+=("scrollbars="+bars+",");   
 65opt+=("resizable="+resize+",");   
 66opt+=("width="+wide+",");   
 67opt+=("height="+hite);   
 68winHandle=window.open(doc,winNew,opt);   
 69return;   
 70}   
 71/*********************************************************************************   
 72* FUNCTION: DecimalFormat   
 73* PARAMETERS: paramValue -&gt; Field value   
 74* CALLS: NONE   
 75* RETURNS: Formated string   
 76**********************************************************************************/   
 77function DecimalFormat (paramValue) {   
 78var intPart = parseInt(paramValue);   
 79var decPart =parseFloat(paramValue) - intPart; 
 80
 81str = "";   
 82if ((decPart == 0) || (decPart == null)) str += (intPart + ".00");   
 83else str += (intPart + decPart); 
 84
 85return (str);   
 86} 
 87
 88"^\\\d+$" //非负整数(正整数 + 0)   
 89"^[0-9]*[1-9][0-9]*$" //正整数   
 90"^((-\\\d+)|(0+))$" //非正整数(负整数 + 0)   
 91"^-[0-9]*[1-9][0-9]*$" //负整数   
 92"^-?\\\d+$" //整数   
 93"^\\\d+( \\\\.\\\d+)?$ " //非负浮点数(正浮点数 + 0)   
 94"^(([0-9]+\\\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\\\.[0-9]+)|([0-9]*[1-9][0-9]*))$" //正浮点数   
 95"^((-\\\d+( \\\\.\\\d+)?)|(0+(\\\\.0+)?))$ " //非正浮点数(负浮点数 + 0)   
 96"^(-(([0-9]+\\\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$" //负浮点数   
 97"^(-?\\\d+)( \\\\.\\\d+)?$ " //浮点数   
 98"^[A-Za-z]+$" //由26个英文字母组成的字符串   
 99"^[A-Z]+$" //由26个英文字母的大写组成的字符串   
100"^[a-z]+$" //由26个英文字母的小写组成的字符串   
101"^[A-Za-z0-9]+$" //由数字和26个英文字母组成的字符串   
102"^\\\w+$" //由数字、26个英文字母或者下划线组成的字符串   
103"^[\\\w-]+( \\\\.[\\\w-]+)*@[\\\w-]+(\\\\.[\\\w-]+)+$ " //email地址   
104"^[a-zA-z]+://( \\\w+(-\\\w+)*)(\\\\.(\\\w+(-\\\w+)*))*(\\\?\\\S*)?$ " //url</thestr.length;></moredate>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus