Javascript 公共类
/******以下所有的部门function 需要jquery支持*************/
//在页面上各个空间的按回车键自动获取焦点(选中) 功能
function nextfocus(curr, next) {
if (curr != null && curr != '' && curr.contains("#") == false) { curr = '#' + curr; }
if (next != null && next != '' && next.contains("#") == false) { next = '#' + next; }
if (curr != null) {
$(curr).keypress(function(e) {
if (e.which == 13) {
$(next).focus();
return false;
}
});
} else {
if (next != null) {
$(next).focus();
return false;
}
}
}

//对代码进行过滤防止因特殊字符引起的数据提交错误.
function htmltoTextsplit(s) {
if (s != '' && s!=null) {
if (s.indexOf("'") != -1) {
var regs = new RegExp("\'", "gi");
s = s.replace(regs, "´");
}
if (s.indexOf("\\") != -1) {
// var rega = new RegExp("\\");
s = s.replace(/\\/g, "/");
}
// if (s.indexOf('<') != -1) { s = s.replace('<', '<'); }
// if (s.indexOf('>') != -1) { s = s.replace('>', '>'); }
return s;
} else {
return '';
}

}
//获取网页地址中参数的值(需要输入网址全字符串)
function GetArgsFromHref(sHref, sArgName) {
var args = sHref.split("?");
var retval = "";
if (args[0] == sHref) //Argument is null
{
return '';
}
var str = args[1];
args = str.split("&");
for (var i = 0; i < args.length; i++) {
str = args[i];
var arg = str.split("=");
if (arg.length <= 1) continue;
if (arg[0] == sArgName) retval = arg[1];
}
if (retval.contains('#')) { retval = retval.replace('#', ''); }
return retval;
}
//查找字符串是否以输入的字符开头
String.prototype.startwith = function(element) {
var l = element.length;
if (this.length > 0) {
if (l > 0) {
if (this.substring(0, l ) == element) {
return true;
}
}
}
return false;
}
//检查给定的字符串中是否包含输入的字符
String.prototype.contains = function(element) {
if (this.length > 0) {
if (element.length > 0) {
if (this.indexOf(element) != -1) {
return true;
}
}
}
return false;
}

//endwith
String.prototype.endwith = function(element) {
if (this.length > 0) {
if (element.length > 0) {
if (this.substring(this.length-element.length)==element) {
return true;
}
}
}
return false;
}
//string.trim
String.prototype.trim = function() {
return this.replace(/(^\s*)|(\s*$)/g, "");
}

//调用string.ltrim(str);
String.prototype.ltrim = function() {
return this.replace(/(^\s*)/g, "");
}
String.prototype.rtrim = function() {
return this.replace(/(\s*$)/g, "");
}
//判断是否是双进度浮点数
//调用 if(str.isdouble()==false){//do some }
String.prototype.isdouble=function(){
var newp=/^(-|\+)?\d+(\.\d+)?$/;
return newp.test(this);
}
String.prototype.trimzero = function() {
var a = this.toString().replace(/(^(-|\+)?\d*\.\d*?)(0*$)/, "$1");
if (a.endwith('.') == true) {
a = a.replace(".", "");
}
return a;
}
String.prototype.isint = function() {
if (this.length < 1) {
return false;
}

if (isNaN(this)) {return false; }

if (parseInt(this,10) < 1) {return false; }
if (parseFloat(this) > parseInt(this, 10)) {return false; }
return true;
} //判断是否为正整数


//判断intY年intM月的天数
//返回值:intY年intM月的总天数
function fnComputerDay(intY, intM) {
var dtmd = new Date(intY, intM, -1);
var intD = dtmd.getDate() + 1;
return intD;
}
/*
去掉双精度或金额数据转化为字符串后后面多余的00
例如 一个金额的正确显示为 10.300
通过使用 "10.300".trimzero() 为 10.3
*/
String.prototype.trimzero = function() {
var a = this.toString().replace(/(^(-|\+)?\d*\.\d*?)(0*$)/, "$1");
if (a.endwith('.') == true) {
a = a.replace(".", "");
}
return a;
}

//判断号码的所属营运商
function getMobileServices(mobile) {
if(mobile.startwith("0")==true){
mobile=mobile.substring(1);
}
if(mobile.startwith("+086")==true){
mobile=mobile.substring(4);
}
var chinaunicom=new Array("130","131","132","133");
var chinamobilea=new Array("135","136","137","138","139","150","158","159");
var chinamobileb=new Array("1340","1341","1342","1343","1344","1345","1346","1347","1348");
var bolchinaunicom=chinaunicom.contains(mobile.substring(0,3));
var bolchinamobilea=chinamobilea.contains(mobile.substring(0,3));
var bochinamobileb=chinamobileb.contains(mobile.substring(0,4));
if(bolchinaunicom)
return 1//联通
if(bolchinamobilea || bochinamobileb )
return 2 //移动
return 3 //电信或其他营运商
}


Last modification:July 14, 2020
如果觉得我的文章对你有用,请随意赞赏