Common JavaScript functions

Source: Internet
Author: User
Tags date format date1 functions integer valid valid email address window
javascript| function

/***************************************************
* Eo_jslib.js
* JavaScript Regular expression test
*******************************************************/


The checksum is all made up of numbers
function IsDigit (s)
{
var patrn=/^[0-9]{1,20}$/;
if (!patrn.exec (s)) return false
return True
}

Verify Login Name: Can only enter 5-20 letters beginning with a letter, can be with numbers, "_", "." The string
function Isregisterusername (s)
{
var patrn=/^[a-za-z]{1} ([a-za-z0-9]|[. _]) {4,19}$/;
if (!patrn.exec (s)) return false
return True
}

Verify user name: You can enter only 1-30 strings that start with a letter
function Istruename (s)
{
var patrn=/^[a-za-z]{1,30}$/;
if (!patrn.exec (s)) return false
return True
}

Verify password: Can only enter 6-20 letters, numbers, underscores
function ispasswd (s)
{
var patrn=/^ (\w) {6,20}$/;
if (!patrn.exec (s)) return false
return True
}

Check ordinary telephone, fax number: You can "+" start, in addition to numbers, can contain "-"
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
}

Check mobile phone Number: Must start with a number, in addition to the number, can contain "-"
function Ismobil (s)
{
var patrn=/^[+]{0,1} (\d) {1,3}[]? ([-]? ((\d) | []) {1,12}) +$/;
if (!patrn.exec (s)) return false
return True
}

Check ZIP code
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
}

Verify Search Keywords
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 Hig Her limit to check
* calls:nothing
* returns:true If Val are 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,)) {return (false);}
Else if (m==4 | | m==6 | | m==9 | | m==11) maxdays = 30;
Else if (m==2) {
if (y% 4 > 0) maxdays =;
else if (y% = = 0 && y% > 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 Otherwi Se false.
*************************************************/
Function iseudate (thestr) {
If Isbetween ( Thestr.length, 8 () = = 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,) = = 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% = = 0 && y% > 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 e-mail address
* FALSE If the passed string is isn't a valid Email address
* EMAIL FORMAT: anyname@emailserver e.g; webmaster@hotmail.com
* @ sign can appear only once to 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+$//non-negative integer (positive integer + 0)
"^[0-9]*[1-9][0-9]*$"//Positive integer
"^ (-\\d+) | ( 0+)) $ "///Non positive integer (negative integer + 0)
" ^-[0-9]*[1-9][0-9]*$ "//Negative integer
" ^-?\\d+$ "//Integer
" ^\\d+ ( \\.\\ d+)? $ "//non-negative floating-point number (positive floating-point number + 0)
" ^ ([0-9]+\\.[ 0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\\. [0-9]+) | ([0-9]*[1-9][0-9]*)] $ "//Positive floating-point number
" ^ (-\\d+ ( \\.\\d+) | (0+ (\\.0+)) $ //non-positive floating-point number (negative floating-point number + 0)
"^ (-([0-9]+\\.[ 0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\\. [0-9]+) | ([0-9]*[1-9][0-9]*))] $ "//negative floating-point number
" ^ (-?\\d+) ( \\.\\d+)? $ '//floating-point number
' ^[a-za-z]+$ '//26 English letters
^[a-z]+$///26-Letter uppercase String
"^[a-z]+$"///26-Letter lowercase string
"^[a-za-z0-9]+$"//by number and 26 English-letter strings
" ^\\w+$ "///a string consisting of numbers, 26 English letters, or underscores
^[\\w-]+ ( \\.[ \\w-]+) *@[\\w-]+ (\\.[ \\w-]+) +$ "//email address
" ^[a-za-z]+://( \\w+ (-\\w+) *) (\ \\w+ (-\\w+) *)) * (\\?\\s*) $ "//url



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.