首頁  >  文章  >  web前端  >  js常用自訂公共函數匯總_基礎知識

js常用自訂公共函數匯總_基礎知識

WBOY
WBOY原創
2016-05-16 17:03:401529瀏覽
複製程式碼 程式碼如下:

String.prototype.trim = function(){
return this.replace(/(^s*)|(s*$)/g, "");
}

//檢查是否為日期格式
function isDate(datestr){
var result = datestr.match(/((^((1[8-9]d{2})|([2 -9]d{3}))(-)(10|12|0?[13578])(-)(3[01]|[12][0-9]|0?[1-9])$ )|(^((1[8-9]d{2})|([2-9]d{3}))(-)(11|0?[469])(-)(30|[12 ][0-9]|0?[1-9])$)|(^((1[8-9]d{2})|([2-9]d{3}))(-)( 0?2)(-)(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)(-)(0? 2)(-)(29)$)|(^([3579][26]00)(-)(0?2)(-)(29)$)|(^([1][89][0 ][48])(-)(0?2)(-)(29)$)|(^([2-9][0-9][0][48])(-)(0?2) (-)(29)$)|(^([1][89][2468][048])(-)(0?2)(-)(29)$)|(^([2-9] [0-9][2468][048])(-)(0?2)(-)(29)$)|(^([1][89][13579][26])(-)(0 ?2)(-)(29)$)|(^([2-9][0-9][13579][26])(-)(0?2)(-)(29)$))/ );
if(result==null){
return "no";
}
return "yes";
}

//此方法上面效果一致
function isDate2(datestr) {
var result = datestr.match(/^(d{1,4})(-|/)(d{1,2})2(d{1,2 })$/);
if (result == null)
return "no";
var d = new Date(result[1], result[3] - 1, result[4]) ;
if((d.getFullYear() == result[1] && (d.getMonth() 1) == result[3] && d.getDate() == result[4])){
return "yes";
}
return "no";
}

//判斷輸入的字元是否為中文
function IsChinese(str){
if (str.length!=0){
reg=/^[u0391-uFFE5] $/;
if(!reg.test(str)){
// alert("對不起,您輸入的字串型別格式不正確!");
return "no";
}
}
return "yes";
}


//判斷是否為空
function isEmpty(str){
if(str==null||typeof str=="undefined"||str.trim()==""){
return true;
}else{
return false;
}
}

//固定電話
function testTelephone(phone){
var phone_reg = new Regone_reg ([ ]{0,1}d{3,4}|d{3,4}-)?d{7,8}$/);
if(!phone_reg.test(phone)){
return "no";
}
return "yes";
}
//折扣
function isDiscount(discount){
var phone_reg = new RegExp(/^(0 ([.]d{1,2})|1|1.00|1.0)$/);
if(!phone_reg.test(discount)){
return "no";
}
return "yes";
}
//手機號碼
function testMobile(mobile){
var mobile_reg = new RegExp(/^0{0,1}1[0-9]{ 10}$/);
if(!mobile_reg.test(mobile)){
return "no";
}
return "yes";
}
//QQ號碼從10000開始
function testQQ(qq){
var qq_reg = new RegExp(/^[1-9][0-9]{4,}$/);
if(!qq_reg. test(qq)){
return "no";
}
return "yes";
}
//電子郵件
function testEmail(email){
var email_reg = new RegExp(/^w ([- .]w )*@w ([-.]w )*.w ([-.]w )*$/);
if(!email_reg.test( email)){
return "no";
}
return "yes";
}

//不帶符號的正整數
function testPlusDigit(digit) {
var plusDigit_reg = new RegExp(/^d $/);
if(!plusDigit_reg.test(digit)){
return "no";
}
return "yes" ;
}

//DOUBLE價
function testPriceFormat(str){
var priceFormatReg = new RegExp(/^d (.d{1,2})?$/);
if(!priceFormatReg.test(str)){
return "no";
}
return "yes";
}

//身分證
function testIDCard(str){
var IDCardReg = new RegExp(/(^d{15}$)|(^d{17}([0-9]|X)$)/);
if (!IDCardReg.test(str)){
return "no";
}
return "yes";
}

//2012-06-19日期格式
function testDate(str){
var dateReg = new RegExp(/(^d{4}-[0,1][0-9]-[0-3][0-9]$)/) ;
if(!dateReg.test(str)){
return "no";
}
return "yes";
}



//浮點數精確運算(加法)
function accAdd(arg1,arg2){
var r1,r2,m,n;
try{r1=arg1.toString().split(". ")[1].length}catch(e){r1=0}
try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0 }
m=Math.pow(10,Math.max(r1,r2));
n=(r1>=r2)?r1:r2;
return ((arg1*m arg2*m )/m).toFixed(n);
}
Number.prototype.add = function (arg){
return accAdd(arg,this);
}

/ /浮點數精確運算(減法)
function accSub(arg1,arg2){
return accAdd(arg1,-arg2);
}
Number.prototype.subtract = function (arg){
return accSub(this,arg);
}

//浮點數精確運算(乘法)
function accMul(arg1,arg2)
{
var m=0 ,s1=arg1.toString(),s2=arg2.toString();
try{m =s1.split(".")[1].length}catch(e){}
try{m =s2.split(".")[1].length}catch(e){}
return Number(s1.replace(".",""))*Number(s2.replace(".", ""))/Math.pow(10,m)
}
Number.prototype.mul = function (arg){
return accMul(arg, this);
}

//浮點數精確運算(除法)
function accDiv(arg1,arg2){
var t1=0,t2=0,r1,r2;
try{t1=arg1.toString() .split(".")[1].length}catch(e){}
try{t2=arg2.toString().split(".")[1].length}catch(e){}
with(Math){
r1=Number(arg1.toString().replace(".",""))
r2=Number(arg2.toString().replace(".", ""))
return (r1/r2)*pow(10,t2-t1);
}
}
Number.prototype.div = function (arg){
return accDiv (this, arg);
}

//限制輸入數字
function isNumber(e) {
if ($.browser.msie) {
if ( ((event.keyCode > 47) && (event.keyCode (event.keyCode == 8) ) {
return true;
} else {
return false;
}
} else {
if ( ((e.which > 47) && (e.which (e.which == 8) ) {
return true;
} else {
return>return true;
} else {
return false;
}
}
}


//字串長度截取
function cutstr(str, len) {
var temp
var icount = 0;
var patrn = /[^x00-xff]/;
var strre = "";
for (var i = 0; i if (icount temp = str.substr(i, 1);
if (patrn.exec(temp) == null) {
icount = icount 1;
} else {
icount = icount 2;
}
strre = temp;
} else {
break
}
}
return strre "...🎜>}
}
return strre "..." ;
}

//取得網域主機
function getHost(url) {
var host = "null";
if (typeof url == "undefined" || null == url) {
url = window.location.href;
}
var regex = /^w ://([^/]*).*/;
var match = url .match(regex);
if (typeof match != "undefined" && null != match) {
host = match[1];
}
return host;
}

//判斷某個值是否在所在範圍
//rang=1 表示正整數[0,2147483647] 2表示float[0,3.4028235E38]
//return= 'empempty'表示輸入為空,
function isRang(str,rang){
if(typeof str == "number"){
var num = Number(str);
//判斷是否在正整數範圍
if( rang == 1){
if(testPlusDigit(num)=="yes"){
if(num>=0&&numreturn "is_int" ;
}else{
return "is_not_int_rang";
}
}else{
return "is_not_int";
}
}else if(rang == 2){
if(testPriceFormat(num)=="yes"){
if(num>=0&&numreturn "is_float";
}else{
return " is_not_float_rang";
}
}else{
return "is_not_float";
}
}else{
return "rang_is_not_right";
}else{
return "rang_is_not_right";
} 🎜>return "is_not_number"; } }
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn