Convenient development of common javascript script Library

Source: Internet
Author: User
Tags rtrim

Save the following code as Common. js
Class library function:
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
7. IsEnLetter (objStr, size) -- verify whether the letter is 26 characters long
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.
7. IsEnLetter (objStr, size) -- verify if it is 26 letters
*/
/*
========================================================== ======================================
String operation
Trim (string): removes spaces on both sides of the string.
========================================================== ======================================
*/
/*
========================================================== ======================================
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;
}
/*
========================================================== ======================================
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;
}
/*
========================================================== ======================================
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 ("&","&");
Str = str. replace ("<","");
Str = str. replace ("'","'");
Str = str. replace ("\"",""");
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;
}
}
/*
IsEnLetter (string, string): test string, Case (UL, U, L or ul, u, l)
*/
Function IsEnLetter (objStr, size)
{
Var reg;
If (Trim (objStr) = "")
{
Return false;
}
Else
{
ObjStr = objStr. toString ();
}
If (size = null) | (Trim (size) = ""))
{
Size = "UL ";
}
Else
{
Size = size. toUpperCase ();
}
Switch (size)
{
Case "UL ":
// Case sensitive
Reg =/^ [A-Za-z] + $ /;
Break;
Case "U ":
// Uppercase
Reg =/^ [A-Z] + $ /;
Break;
Case "L ":
// Lower case
Reg =/^ [a-z] + $ /;
Break;
Default:
Alert ("Check case parameters, can only be (null, UL, U, L )");
Return false;
Break;
}
Var r = objStr. match (reg );
If (r = null)
{
Return false;
}
Else
{
Return true;
}
}
/*
========================================================== ==================================
Function: Tips
Author: Shen Wang
Date: 2004/04/15
========================================================== ====================================
*/
// Define variables and set default values
Var LabelFontFace = ", arial, Verdana ";
Var LabelFontColor = "#000000 ";
Var LabelFontSize = "9pt ";
Var LabelFontStyle = "Font. PLAIN ";
Var LabelBorderColor = "#000000 ";
Var LabelBackColor = "# FFFFE1 ";
// Set attributes
Function SetLabelFontFace (obj)
{
Obj = Trim (obj );
If (obj = null | obj = "")
{
Obj = ", arial, Verdana ";
}
LabelFontFace = obj;
}
Function SetLabelFontColor (obj)
{
Obj = Trim (obj );
If (obj = null | obj = "")
{
Obj = "#000000 ";
}
LabelFontColor = obj;
}
Function SetLabelFontSize (obj)
{
Obj = Trim (obj );
If (obj = null | obj = "")
{
Obj = "9pt ";
}
LabelFontSize = obj;
}
Function SetLabelFontStyle (obj)
{
Obj = Trim (obj );
If (obj = null | obj = "")
{
Obj = "Font. PLAIN ";
}
LabelFontStyle = obj;
}
Function SetLabelBorderColor (obj)
{
Obj = Trim (obj );
If (obj = null | obj = "")
{
Obj = "#000000 ";
}
LabelBorderColor = obj;
}
Function SetLabelBackColor (obj)
{
Obj = Trim (obj );
If (obj = null | obj = "")
{
Obj = "# FFFFE1 ";
}
LabelBackColor = obj;
}
// Composite text style
Function SetTextStyle (str)
{
Var strRet = "";
Var strStyle = "";
StrStyle = "font-family:" + LabelFontFace + ";";
StrStyle + = "color:" + LabelFontColor + ";";
StrStyle + = "font-size:" + LabelFontSize + ";";
Switch (LabelFontStyle. toLowerCase ())
{
Case "font. plain ":
StrStyle + = "font-weight: normal ;";
StrStyle + = "font-style: normal ;";
Break;
Case "font. bold ":
StrStyle + = "font-weight: bold ;";
StrStyle + = "font-style: normal ;";
Break;
Case "font. italic ":
StrStyle + = "font-weight: normal ;";
StrStyle + = "font-style: italic ;";
Break;
Case "font. italicbold ":
Case "font. bolditalic ":
StrStyle + = "font-weight: bold ;";
StrStyle + = "font-style: italic ;";
Break;
Default:
StrStyle + = "font-weight: bold ;";
StrStyle + = "font-style: italic ;";
Break;
}
StrRet = "<font style = '" + strStyle + "'> ";
StrRet + = "" + str + "";
StrRet + = "</font> ";
Return strRet;
}
// Synthesize Table Styles
Function SetTableStyle ()
{
Var strRet = "";
StrRet + = "border-right:" + LabelBorderColor + "1px solid ;";
StrRet + = "border-top:" + LabelBorderColor + "1px solid ;";
StrRet + = "border-left:" + LabelBorderColor + "1px solid ;";
StrRet + = "border-bottom:" + LabelBorderColor + "1px solid ;";
StrRet + = "background-color:" + LabelBackColor;
Return strRet;
}
// Display prompt
Function ShowNote (str)
{
Var strHtml;
StrHtml = "";
StrHtml + = "<table height = 1px width = 1px border = '0' cellspacing = '0' cellpadding = '0' style = '" + SetTableStyle () + "'> ";
StrHtml + = "<tr> ";
StrHtml + = "<td>" + SetTextStyle (str) + "</td> ";
StrHtml + = "</tr> ";
StrHtml + = "</table> ";
If (document. all & document. readyState = "complete ")
{
Document. all. div_Note.innerHTML = strHtml;
Document. all. div_Note.style.pixelLeft = event. clientX + document. body. scrollLeft + 10
Document. all. div_Note.style.pixelTop = event. clientY + document. body. scrollTop + 10
Document. all. div_Note.style.visibility = "visible"
}
}
// Hide the prompt
Function HideNote ()
{
If (document. all)
{
Document. all. div_Note.style.visibility = "hidden ";
}
Else
{
If (document. layers)
{
ClearInterval (currentscroll)
Document. div_Note.visibility = "hidden ";
}
}
}
// Initialization
Function Init ()
{
Invalid parameter Doc ument. write ("<div id = \" div_Note \ "style = \" VISIBILITY: hidden; POSITION: absolute; HEIGHT: 13px; z-index: 1 \ "> </div> ");
}
Init ();
// Generate the prompt character
Function ShowLabel (text, note, bclick)
{
If (bclick! = Null)
{
Return "<a href = \" # \ "onMouseOver = \" ShowNote ('"+ note +"') \ "onMouseOut = \" HideNote () \ "onClick = \" javascriptoSomeThing (this); \ ">" + text + "</a> ";
}
Else
{
Return "<a href = \" # \ "onMouseOver = \" ShowNote ('"+ note +"') \ "onMouseOut = \" HideNote () \ ">" + text + "</a> ";
}
}
Test page: Copy codeThe Code is as follows: <HTML>
<HEAD>
<Title> Common javascript </title>
<Script language = "javascript" src = "./Common. js"> </script>
<Script language = "javascript">
Function CheckInt ()
{
IptResult. value = IsInt (iptTest. value, iptSign. value, iptZero. value );
}
Function CheckFloat ()
{
IptResult. value = IsFloat (iptTest. value, iptSign. value, iptZero. value );
}
Function CheckLetter ()
{
IptResult. value = IsEnLetter (iptTest. value, iptSign. value );
}
Document. write (ShowLabel ("TEST", "Only a testing! "));
Document. write ("<br> ");
</Script>
<Meta http-equiv = "expires" content = "0">
</HEAD>
<Body>
<Input type = text value = "" id = "iptTest"> <input type = button value = "IsEmpty" onclick = "IsEmpty ('ipttest');">
<Input type = button value = "CheckInt" onclick = "CheckInt ()">
<Br>
<Input type = text value = "" id = "iptSign" NAME = "iptSign"> <input type = button value = "CheckFloat" onclick = "CheckFloat ()">
<Br>
<Input type = text value = "" id = "iptZero" NAME = "iptZero"> <input type = button value = "CheckLetter" onclick = "CheckLetter ()">
<Br>
<Input type = text value = "" id = iptResult disabled = true>
</Body>
</HTML>
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.