JS Classic Source: Generic JavaScript script function library

Source: Internet
Author: User
Tags empty integer rtrim tostring trim
javascript|js| Function | Scripting JavaScript can play a big role in web programming by writing some of the most common features into JavaScript libraries.

Save the following code as Common.js

Class Library Features:

  1.Trim (str)--Remove the spaces on either side of the string

  2.XMLEncode (str)--XML encoding of strings

  3.ShowLabel (STR,STR)--Mouse prompt function (display character, prompt character)

You can set the font, color, size, and hint background color, border, and so on for the text displayed

  4.IsEmpty (obj)--Verifying that the input box is empty

  5.IsInt (Objstr,sign,zero)--verifies if it is an integer, a positive integer, a negative integer, and whether it includes 0

  6.IsFloat (Objstr,sign,zero)--Verify that the floating-point number, positive floating-point, negative floating-point, and whether to include 0

  7.IsEnLetter (objstr,size)--Verify if 26 letters, small capitalization

The source code is as follows:

/*

Name: Common.js

Function: Generic JavaScript script function library

Including:

1.Trim (str)--Remove the spaces on either side of the string

2.XMLEncode (str)--XML encoding of strings

3.ShowLabel (STR,STR)--Mouse prompt function (display character, prompt character)

4.IsEmpty (obj)--Verifying that the input box is empty

5.IsInt (Objstr,sign,zero)--Verifying whether an integer

6.IsFloat (Objstr,sign,zero)--Verify that it is a floating-point number

7.IsEnLetter (objstr,size)--Verify that it is 26 letters

*/

/* String operation

Trim (String): Remove spaces on both sides of a string

*/

/*

1. LTrim (String): Removing space on the left

*/

function LTrim (str)

{

var whitespace = new String ("\t\n\r");

var s = new String (str);

if (Whitespace.indexof (S.charat (0))!=-1)

{

var j=0, i = s.length;

while (J < i && Whitespace.indexof (S.charat (j))!=-1)

{

j + +;

}

s = s.substring (j, I);

}

return s;

}

/*

2. RTrim (String): Remove the space on the right

*/

function RTrim (str)

{

var whitespace = new String ("\t\n\r");

var s = new String (str);

if (Whitespace.indexof (S.charat (s.length-1))!=-1)

{

var i = s.length-1;

while (i >= 0 && whitespace.indexof (s.charat (i))!=-1)

{

i--;

}

s = s.substring (0, i+1);

}

return s;

}

/*

3. Trim (String): Remove before and after spaces

*/

function Trim (str)

{

Return RTrim (LTrim (str));

}

/*

Xmlencode (String): XML Encoding of strings

*/

function Xmlencode (str)

{

Str=trim (str);

Str=str.replace ("&", "&");

Str=str.replace ("<", "<");

Str=str.replace (">", ">");

Str=str.replace ("'", "" ");

Str=str.replace ("\" "," "");

return str;

}

/*

Validating class functions

*/

function IsEmpty (obj)

{

Obj=document.getelementsbyname (obj). Item (0);

if (Trim (obj.value) = "")

{

Alert ("field cannot be empty.) ");

if (Obj.disabled==false && obj.readonly==false)

{

Obj.focus ();

}

}

}

/*

Isint (String,string,int or string):(test string, + or-or empty,empty or 0)

Function: To determine whether an integer, positive integer, negative integer, positive integer +0, negative integer +0

*/

function Isint (Objstr,sign,zero)

{

var reg;

var Bolzero;

if (Trim (OBJSTR) = "")

{

return false;

}

Else

{

Objstr=objstr.tostring ();

}

if ((sign==null) | | (Trim (sign) = ""))

{

Sign= "+-";

}

if ((zero==null) | | (Trim (zero) = "")

{

Bolzero=false;

}

Else

{

Zero=zero.tostring ();

if (zero== "0")

{

Bolzero=true;

}

Else

{

Alert ("Check for 0 parameters, only (empty, 0)");

}

}

Switch (sign)

{

Case "+-":

Integer

reg=/(^-?| ^\+?) \d+$/;

Break

Case "+":

if (!bolzero)

{

Positive integer

Reg=/^\+? [0-9]*[1-9][0-9]*$/;

}

Else

{

Positive integer +0

reg=/^\+?\d+$/;

Reg=/^\+? [0-9]*[0-9][0-9]*$/;

}

Break

Case "-":

if (!bolzero)

{

Negative integer

reg=/^-[0-9]*[1-9][0-9]*$/;

}

Else

{

Negative integer +0

reg=/^-\d+$/;

reg=/^-[0-9]*[0-9][0-9]*$/;

}

Break

Default

Alert ("Check symbol parameters, only (empty, +,-)");

return false;

Break

}

var r=objstr.match (REG);

if (r==null)

{

return false;

}

Else

{

return true;

}

}

/*

Isfloat (String,string,int or string):(test string, + or-or empty,empty or 0)

Function: To determine whether it is floating point number, positive floating-point number, negative floating-point number, positive floating-point number +0, negative float +0

*/

function Isfloat (Objstr,sign,zero)

{

var reg;

var Bolzero;

if (Trim (OBJSTR) = "")

{

return false;

}

Else

{

Objstr=objstr.tostring ();

}

if ((sign==null) | | (Trim (sign) = ""))

{

Sign= "+-";

}

if ((zero==null) | | (Trim (zero) = "")

{

Bolzero=false;

}

Else

{

Zero=zero.tostring ();

if (zero== "0")

{

Bolzero=true;

}

Else

{

Alert ("Check for 0 parameters, only (empty, 0)");

}

}

Switch (sign)

{

Case "+-":

Floating point numbers

reg=/^ ((-?| \+?) \d+) (\.\d+) $/;

Break

Case "+":

if (!bolzero)

{

Positive floating-point numbers

Reg=/^\+? ([0-9]+\. [0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\. [0-9]+) | ([0-9]*[1-9][0-9]*)] $/;

}

Else

{

Positive floating-point number +0

Reg=/^\+?\d+ (\.\d+) $/;

}

Break

Case "-":

if (!bolzero)

{

Negative floating-point numbers

reg=/^-([0-9]+\.[ 0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\. [0-9]+) | ([0-9]*[1-9][0-9]*)] $/;

}

Else

{

Negative floating-point number +0

reg=/^ (-\d+ (\.\d+)?) | (0+ (\.0+)) $/;

}

Break

Default

Alert ("Check symbol parameters, only (empty, +,-)");

return false;

Break

}

var r=objstr.match (REG);

if (r==null)

{

return false;

}

Else

{

return true;

}

}



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.