Typical JS source code: Common Javascript script Library

Source: Internet
Author: User
Tags rtrim
  1. Trim (STR) -- remove spaces on both sides of the string

  2. xmlencode (STR) -- encode the string in XML format.

  3. showlabel (STR, STR) -- mouse prompt function (display character, prompt character)

You can set the font, color, size, background color, and border of the displayed prompt text.

  4. isempty (OBJ) -- verify that the input box is empty

  5. isint (objstr, sign, zero) -- verify whether it is an integer, positive integer, negative integer, and whether it includes zero

  6. isfloat (objstr, sign, zero) -- verify whether it is a floating point, Positive floating point, negative floating point, and whether it includes zero

The source code is as follows:

/*

Name: Common. js

Function: Common Javascript script Library

Including:

1. Trim (STR) -- remove spaces on both sides of the string

2. xmlencode (STR) -- encode the string in XML format.

3. showlabel (STR, STR) -- mouse prompt function (display character, prompt character)

4. isempty (OBJ) -- verify that the input box is empty

5. isint (objstr, sign, zero) -- verify whether it is an integer

6. isfloat (objstr, sign, zero) -- verify whether it is a floating point number.

*/

/* String operation

Trim (string): removes spaces on both sides of the string.

*/

/*
1. ltrim (string): removes spaces 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): removes spaces 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): removes leading and trailing spaces.
*/

Function trim (STR)

{

Return rtrim (ltrim (STR ));

}

/*

Xmlencode (string): encode the string in XML format.

*/

Function xmlencode (STR)

{

STR = trim (STR );

STR = Str. Replace ("&", "& amp ;");

STR = Str. Replace ("<", "& lt ;");

STR = Str. Replace (">", "& gt ;");

STR = Str. Replace ("'", "& apos ;");

STR = Str. Replace ("\" "," & quot ;");

Return STR;

}

/*
Verification functions
*/

Function isempty (OBJ)

{

OBJ = Document. getelementsbyname (OBJ). Item (0 );

If (TRIM (obj. Value) = "")

{

Alert ("field cannot be blank. ");

If (obj. Disabled = false & obj. readonly = false)

{

OBJ. Focus ();

}

}

}

 

/*
Isint (string, String, Int or string) :( test string, + or-or empty, empty or 0)

Function: determines whether it is 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 whether the parameter 0 is included, can only be (null, 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, which can only be (null, + ,-)");

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: determines whether it is a floating point number, Positive floating point number, negative floating point number, Positive floating point number + 0, negative floating point number + 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 whether the parameter 0 is included, can only be (null, 0 )");

}

}

Switch (sign)

{

Case "+ -":

// Floating point number

Reg =/^ ((-? | \ + ?) \ D +) (\. \ D + )? $ /;

Break;

Case "+ ":

If (! Bolzero)

{

// Positive floating point number

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 number

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, which can only be (null, + ,-)");

Return false;

Break;

}

VaR r = objstr. Match (REG );

If (r = NULL)
{
Return false;
}
Else
{
Return true;
}
}

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.