Home  >  Article  >  Web Front-end  >  Universal javascript script function library to facilitate development_javascript skills

Universal javascript script function library to facilitate development_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:44:48841browse

Save the following code as Common.js
Class library functions:
1.Trim(str)--Remove spaces on both sides of the string
2.XMLEncode(str)--Encode the string to XML
3.ShowLabel(str,str)——Mouse prompt function (display characters, prompt characters)
You can set the font, color, size of the displayed prompt text, as well as the prompt background color, border, etc.
4 .IsEmpty(obj)——Verify whether 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 number, positive floating point, negative floating point, and whether it includes zero
7.IsEnLetter(objStr,size)--verify whether it is 26 letters, uppercase and lowercase
The source code is as follows:
/*
Name: Common.js
Function: Universal javascript script function library
Includes:
1.Trim(str)--Remove spaces on both sides of the string
2.XMLEncode(str)--XML encoding of strings
3.ShowLabel(str,str)--mouse prompt function (display characters, prompt characters)
4.IsEmpty(obj)- -Verify whether 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 whether it is 26 letters
*/
/*
====================== ============================================
String operations
Trim(string): Remove spaces on both sides of the string
==================================== =================================
*/
/*
==== ================================================== ============
LTrim(string): Remove spaces on the left
======================== ===========================================
*/
function LTrim(str)
{
var whitespace = new String(" tnr");
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 spaces on the right
==================== ===============================================
*/
function RTrim(str)
{
var whitespace = new String(" tnr");
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 leading and trailing spaces
========== ================================================== ======
*/
function Trim(str)
{
return RTrim(LTrim(str));
}
/*
=== ================================================== =================
XMLEncode(string): XML encoding of the string
================ ================================================== ====
*/
function XMLEncode(str)
{
str=Trim(str);
str=str.replace("&","&");
str=str.replace("<","");
str=str.replace("'","'");
str=str.replace(""","" ");
return str;
}
/*
============================== ================================================== =
Verification class function
============================================ ========================================
*/
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: determine 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 it contains 0 parameters, only (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 symbolic parameters, only (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: Determine 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 it contains 0 parameters, which 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 symbolic parameters, only (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
reg=/ ^[A-Za-z] $/;
break;
case "U":
//uppercase
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;
}
}
/*
======= ================================================== ============
Function: Mouse 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 each attribute
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;
}
//合成文字样式
function SetTextStyle(str)
{
var strRet="";
var str;
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="";
strRet =" " str " ";
strRet ="
";
return strRet;
}
//合成表格样式
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;
}
//显示提示
function ShowNote(str)
{
var strHtml;
strHtml="";
strHtml ="

";
strHtml ="";
strHtml ="";
strHtml ="";
strHtml ="
" SetTextStyle(str) "
";
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"
}
}
//隐藏提示
function HideNote()
{
if (document.all)
{
document.all.div_Note.style.visibility="hidden";
}
else
{
if (document.layers)
{
clearInterval(currentscroll)
document.div_Note.visibility="hidden";
}
}
}
//初始化
function Init()
{
window.document.write("");
}
Init();
//生成提示字符
function ShowLabel(text,note,bclick)
{
if(bclick!=null)
{
return "" text "";
}
else
{
return "" text "";
}
}
测试页面:

复制代码 代码如下:



Common javascript


















Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn