熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Javascript >> 正文

常用的Javascript函數

2022-06-13   來源: Javascript 

  /***************************************************
* EO_JSLibjs
* javascript正則表達式檢驗
*******************************************************/

  //校驗是否全由數字組成
function isDigit(s)
{
var patrn=/^[]{}$/;
if (!patrnexec(s)) return false
return true
}

  //校驗登錄名只能輸入個以字母開頭可帶數字_的字串
function isRegisterUserName(s)
{
var patrn=/^[azAZ]{}([azAZ]|[_]){}$/;
if (!patrnexec(s)) return false
return true
}

  //校驗用戶姓名只能輸入個以字母開頭的字串
function isTrueName(s)
{
var patrn=/^[azAZ]{}$/;
if (!patrnexec(s)) return false
return true
}

  //校驗密碼只能輸入個字母數字下劃線
function isPasswd(s)
{
var patrn=/^(\w){}$/;
if (!patrnexec(s)) return false
return true
}

  //校驗普通電話傳真號碼可以+開頭除數字外可含有
function isTel(s)
{
//var patrn=/^[+]{}(\d){}[ ]?([]?(\d){})+$/;
var patrn=/^[+]{}(\d){}[ ]?([]?((\d)|[ ]){})+$/;
if (!patrnexec(s)) return false
return true
}

  //校驗手機號碼必須以數字開頭除數字外可含有
function isMobil(s)
{
var patrn=/^[+]{}(\d){}[ ]?([]?((\d)|[ ]){})+$/;
if (!patrnexec(s)) return false
return true
}

  //校驗郵政編碼
function isPostalCode(s)
{
//var patrn=/^[azAZ]{}$/;
var patrn=/^[azAZ ]{}$/;
if (!patrnexec(s)) return false
return true
}

  //校驗搜索關鍵字
function isSearch(s)
{
var patrn=/^[^`~!@#$%^&*()+=|\\\][\]\{\}:;\\<>/?]{}[^`~!@$%^&()+=|\\\][\]\{\}:;\\<>?]{}$/;
if (!patrnexec(s)) return false
return true
}

  function isIP(s) //by zergling
{
var patrn=/^[]{}$/;
if (!patrnexec(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 thest = theStrindexOf();
var thend = theStrlastIndexOf();

  if (thest == thend) { return(false); }
else {
var y = theStrsubstring(thest);
var m = theStrsubstring(thest+thend);
var d = theStrsubstring(thend+theStrlength);
var maxDays = ;

  if (isInt(m)==false || isInt(d)==false || isInt(y)==false) {
return(false); }
else if (ylength < ) { return(false); }
else if (!isBetween (m )) { return(false); }
else if (m== || m== || m== || m==) maxDays = ;
else if (m==) {
if (y % > ) maxDays = ;
else if (y % == && y % > ) maxDays = ;
else maxDays = ;
}
if (isBetween(d 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(theStrlength ) == false) { return(false); }
else {
var thest = theStrindexOf(/);
var thend = theStrlastIndexOf(/);

  if (thest == thend) { return(false); }
else {
var m = theStrsubstring(thest+thend);
var d = theStrsubstring(thest);
var y = theStrsubstring(thend+theStrlength);
var maxDays = ;

  if (isInt(m)==false || isInt(d)==false || isInt(y)==false) {
return(false); }
else if (ylength < ) { return(false); }
else if (isBetween (m ) == false) { return(false); }
else if (m== || m== || m== || m==) maxDays = ;
else if (m==) {
if (y % > ) maxDays = ;
else if (y % == && y % > ) maxDays = ;
else maxDays = ;
}

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

  }
/**************************************************
* FUNCTION: Compare Date! Which is the latest!
* PARAMETERS: lessDatemoreDate AS String
* CALLS: isDateisBetween
* RETURNS: TRUE if lessDate<moreDate
****************************************************/
function isComdate (lessDate moreDate)
{
if (!isDate(lessDate)) { return(false);}
if (!isDate(moreDate)) { return(false);}
var lessst = lessDateindexOf();
var lessnd = lessDatelastIndexOf();
var morest = moreDateindexOf();
var morend = moreDatelastIndexOf();
var lessy = lessDatesubstring(lessst);
var lessm = lessDatesubstring(lessst+lessnd);
var lessd = lessDatesubstring(lessnd+lessDatelength);
var morey = moreDatesubstring(morest);
var morem = moreDatesubstring(morest+morend);
var mored = moreDatesubstring(morend+moreDatelength);
var Date = new Date(lessylessmlessd);
var Date = new Date(moreymoremmored);
if (Date>Date) { return(false);}
return(true);

  }

  /**********************************************
* FUNCTION isEmpty checks if the parameter is empty or null
* PARAMETER str AS String
*************************************************/
function isEmpty (str) {
if ((str==null)||(strlength==)) 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=; i<theStrlength; i++) {
if (isDigit(theStrsubstring(ii+)) == 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 dotst = theStrindexOf();
var dotnd = theStrlastIndexOf();
var OK = true;

  if (isEmpty(theStr)) return false;

  if (dotst == ) {
if (!isInt(theStr)) return(false);
else return(true);
}

  else if (dotst != dotnd) return (false);
else if (dotst==) return (false);
else {
var intPart = theStrsubstring( dotst);
var decPart = theStrsubstring(dotnd+);

  if (decPartlength > 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 eg; w
* @ sign can appear only once in the email address
*******************************************************/
function isEmail (theStr) {
var atIndex = theStrindexOf(@);
var dotIndex = theStrindexOf( atIndex);
var flag = true;
theSub = theStrsubstring( dotIndex+)

  if ((atIndex < )||(atIndex != theStrlastIndexOf(@))||(dotIndex < atIndex + )||(theStrlength <= theSublength))
{ 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 > Scroll bars = YES Scroll Bars = NO
resize > Resizable = YES Resizable = NO
* CALLS: NONE
* RETURNS: New window instance
*************************************************************/
function newWindow (doc hite wide bars resize) {
var winNew=_blank;
var opt=toolbar=location=directories=status=menubar=;
opt+=(scrollbars=+bars+);
opt+=(resizable=+resize+);
opt+=(width=+wide+);
opt+=(height=+hite);
winHandle=windowopen(docwinNewopt);
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 == ) || (decPart == null)) str += (intPart + );
else str += (intPart + decPart);

  return (str);
}

  ^\\d+$  //非負整數(正整數 +
^[]*[][]*$  //正整數
^((\\d+)|(+))$  //非正整數(負整數 +
^[]*[][]*$  //負整數
^?\\d+$    //整數
^\\d+(\\\\d+)?$  //非負浮點數(正浮點數 +
^(([]+\\[]*[][]*)|([]*[][]*\\[]+)|([]*[][]*))$  //正浮點數
^((\\d+(\\\\d+)?)|(+(\\+)?))$  //非正浮點數(負浮點數 +
^((([]+\\[]*[][]*)|([]*[][]*\\[]+)|([]*[][]*)))$  //負浮點數
^(?\\d+)(\\\\d+)?$  //浮點數
^[AZaz]+$  //由個英文字母組成的字符串
^[AZ]+$  //由個英文字母的大寫組成的字符串
^[az]+$  //由個英文字母的小寫組成的字符串
^[AZaz]+$  //由數字和個英文字母組成的字符串
^\\w+$  //由數字個英文字母或者下劃線組成的字符串
^[\\w]+(\\[\\w]+)*@[\\w]+(\\[\\w]+)+$    //email地址
^[azAz]+://(\\w+(\\w+)*)(\\(\\w+(\\w+)*))*(\\?\\S*)?$  //url


From:http://tw.wingwit.com/Article/program/Java/Javascript/201311/25473.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.