JS Common regular: Commonly used JS

Source: Internet
Author: User
Tags format key return string trim

--------------------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

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.