General JavaScript script function library facilitates development of _javascript techniques

Source: Internet
Author: User
Tags rtrim visibility
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 manipulation
Trim (String): Remove spaces on both sides of a string
==================================================================
*/
/*
==================================================================
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;
}
/*
==================================================================
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;
}
/*
==================================================================
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 ("\" "," "");
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;
}
}
/*
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
reg=/^[a-za-z]+$/;
Break
Case "U":
Capital
reg=/^[a-z]+$/;
Break
Case "L":
Lowercase
reg=/^[a-z]+$/;
Break
Default
Alert ("Check the case parameters, only (empty, UL, U, L)");
return false;
Break
}
var r=objstr.match (REG);
if (r==null)
{
return false;
}
Else
{
return true;
}
}
/*
=====================================================================
Features: Small mouse tips
Author: Shenwan
Date: 2004/04/15
======================================================================
*/
Defining variables, setting default values
var labelfontface= "song Body, Arial,verdana";
var labelfontcolor= "#000000";
var labelfontsize= "9pt";
var labelfontstyle= "Font.plain";
var labelbordercolor= "#000000";
var labelbackcolor= "#FFFFE1";
Set individual properties
function Setlabelfontface (obj)
{
Obj=trim (obj);
if (Obj==null | | obj== "")
{
Obj= "Song Body, 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;
}
Synthetic text styles
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;
}
Synthetic 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;
}
Show Tips
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 Hints
function Hidenote ()
{
if (document.all)
{
Document.all.div_note.style.visibility= "hidden";
}
Else
{
if (document.layers)
{
Clearinterval (Currentscroll)
Document.div_note.visibility= "hidden";
}
}
}
Class
function Init ()
{
Window.document.write ("<div id=\" div_note\ "style=\" Visibility:hidden; Position:absolute; Height:13px;z-index:1\ "></div>");
}
Init ();
Generate prompt characters
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 (' "+" + ") \" Onmouseout=\ "hidenote () \" > "+ text +" </a> ";
}
}
Test page:
Copy Code code 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.