常用JS
[javascript]
//--------------------以下通用函数
/**
* format方法。
* eg:
*两种调用方式
*var template1="我是{0},今年{1}了";
*var template2="我是{name},今年{age}了";
*var result1=template1.format("loogn",22);
*var result2=template1.format({name:"loogn",age:22});
*两个结果都是"我是loogn,今年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);
}
}
/*
* 判空。 左右空白验证时会忽略。
*/
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;
};
//去空格
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(/\s+$/g, "");};
//验证手机号
String.prototype.isMobile = function() {
var re = /^0?(13[0-9]|15[012356789]|18[0236789]|14[57])[0-9]{8}$/;
return (re.test(this.Trim()));
};
//验证电话
//:/^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/
///^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/ //"兼容格式: 国家代码(2到3位)-区号(2到3位)-电话号码(7到8位)-分机号(3位)"
String.prototype.isTel = function(){
return (/^((0\d{2,3})-)(\d{7,8})?$/.test(this.Trim()));
};
//--------------------以下通用函数
/**
* format方法。
* eg:
*两种调用方式
*var template1="我是{0},今年{1}了";
*var template2="我是{name},今年{age}了";
*var result1=template1.format("loogn",22);
*var result2=template1.format({name:"loogn",age:22});
*两个结果都是"我是loogn,今年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 (&qu
补充:web前端 , JavaScript ,