JS Common regular: Commonly used JS
Last Update:2017-02-28
Source: Internet
Author: User
--------------------the following common functions
/**
* Format method.
* Eg:
* Two ways of calling
*var template1= "I am {0}, this year {1}";
*var template2= "I am {name}, this year {age}";
*var Result1=template1.format ("Loogn", 22);
*var Result2=template1.format ({name: "Loogn", age:22});
* Two results are "I'm loogn, this is 22."
* @param {} args
* @return {String}
*/
String.prototype.format = function (args) {
if (arguments.length>0) {
var result = this;
if (arguments.length = = 1 && typeof (args) = = "Object") {
for (var key in args) {
var reg=new RegExp ("({" +key+ "})", "G");
result = Result.replace (Reg, Args[key]);
}
}
else {
for (var i = 0; i < arguments.length; i++) {
if (arguments[i]==undefined)
{
Return "";
}
Else
{
var reg=new RegExp ("({[" +i+ "]})", "G");
result = Result.replace (Reg, arguments[i]);
}
}
}
return result;
}
else {
return this;
}
};
var errorHtml = ' <b id= ' {id}_error ' class= ' unserrorclass ' style= ' color:red; >{message}</b> ';
var errorhtml_br = ' <br/><b id= "{id}_error" class= "Unserrorclass" style= "color:red;" >{message}</b> ';
function ShowMessage (p_id, p_message) {
Farmatmessage (1, p_id, p_message);
}
function Cleanmessage (p_id) {
$ ("#" + p_id + "_error"). html (');
$ ("#" + p_id + "_error"). Remove ();
}
function Showbrmessage (p_id, p_message) {
Farmatmessage (2, p_id, p_message);
}
function farmatmessage (format, p_id, P_message) {
var error;
if (format = = 1) {
Error = Errorhtml.format ({id:p_id, message:p_message});
} else {
Error = Errorhtml_br.format ({id:p_id, message:p_message});
}
if ($ ("#" + p_id + "_error"). Length > 0) {
$ ("#" + p_id + "_error"). HTML (p_message);
} else {
$ ("#" + p_id). After (error);
}
}
/*
* A short sentence. Left and right blank validation is ignored.
*/
function IsNull (str) {
if (str!= null && str!= ' "
&& Str. Trim ()!= ' ") {
return false;
}
return true;
}
String.prototype.isNull = function () {
if (this!= null && this!= '
&& this. Trim ()!= ' ") {
return false;
}
return true;
};
Go to Space
String.prototype.Trim = function () {
var m = This.match (/^\s* (\s+ (\s+\s+) *) \s*$/);
return (M = = null)? "": m[1];
};
String.prototype.Ltrim = function () {return this.replace (/^\s+/g, "");
String.prototype.Rtrim = function () {return this.replace this article link http://www.cxybl.com/html/wyzz/JavaScript_Ajax/ 20130903/40027.html