//格式化小數點數位元,可以指定小數位數,是否四捨五入等參數
Number.prototype.FormatRadix = function (len, IsRound) {
var num = this.toString();
var numArr = num.split('.');
var rad = 0;
var numpart = parseInt(numArr[0]);
if (numArr.length >= 2) {
if (numArr[1].length
rad = parseInt(numArr[1]).FormatLen(len, 1);
}
else {
if (numArr[1].length == len) {
rad = numArr[1];
}
else {
rad = numArr[1].substr(0, len);
if (IsRound) {
var d = parseInt(numArr[1].substr(len, 1));
}
}
}
}
else {
rad = rad.FormatLen(len);
}
return numpart "." rad;
}
複製程式碼 程式碼如下:
//偵測字串中是否有相同的元素split是字串分隔符,如果指定了分隔符,則判斷分隔符為分隔的字串是否有重複,如果沒指定則判斷單一字串是否有重複
//有重複回傳true
String.prototype.CompareElement = function (s) {
var str = this.toString();
if (s == undefined) {
for (var i = 0; i
for (j = i 1; j
if (str.substr(i, 1) == str.substr(j, 1)) {
return true;
}
}
}
}
else {
var strArr = str.split(s);
for (var i = 0; i
for (var j = i 1; j
if (strArr[i] == strArr[j]) {
return true;
}
}
}
}
return false;
}
String.prototype.replaceAll = function (str, tostr) {
oStr = this;
while (oStr.indexOf(str) > -1) {
oStr = oStr.replace(str, tostr);
}
return oStr;
}
Array.prototype.CompareElement = function () {
var strArr = this;
for (var i = 0; i
for (var j = i 1; j
if (strArr[i] == strArr[j]) {
return true;
}
}
}
return false;
}
//字串轉組數,如果未指定分隔符號s,則預設以,分隔分隔符號,如果指定分隔符號為空則將每個字元作為一個陣列元素
String.prototype.ToArray = function (s) {
if (s == undefined) { s = ","; }
var strArr = [];
strArr = this.split(s);
return strArr;
}
//將一個陣列轉換一個字串,所有元素使用指定的分隔符號連接,預設分隔為,
Array.prototype.ToIDList = function (s) {
if (s == undefined) { s = ","; }
var list = "";
for (var i = 0; i
list = (list == "" ? this[i] : s "" this[i]);
}
return list;
}
//取得指定元素的位置索引,元素不存在回傳-1
Array.prototype.GetIndex = function (s) {
var index = -1;
for (var i = 0; i
if ((s "") == this[i]) {
index = i;
}
}
return index;
}
//將指定元素從陣列中刪除
Array.prototype.Remove = function (s) {
var list = "";
for (var i = 0; i
if (s != this[i]) {
list = (list == "" ? this[i] : "," this[i]);
}
}
return list.ToArray();
}
///將陣列進行數字排序asc指定是否進行升序排序,可以為true或false,未指定為升序
Array.prototype.SortByNumber = function (asc) {
if (asc == undefined) { asc = true; }
if (asc) {
return this.sort(SortNumberAsc);
}
else {
return this.sort(SortNumberDesc);
}
}
Array.prototype.InArray = function (e) {
var IsIn = false;
for (var i = 0; i
if (this[i] == (e "")) {
IsIn = true;
}
}
return IsIn;
}
String.prototype.Trim = function (s) { return Trim(this, s); }
String.prototype.LTrim = function (s) { return LTrim(this, s); }
String.prototype.RTrim = function (s) { return RTrim(this, s); }
//搭配Array.SortByNumer使用,將數字進行陣列降序排序
function SortNumberDesc(a, b) {
return b - a;
}
//搭配Array.SortByNumer使用,將數字進行數組升序排序
function SortNumberAsc(a, b) {
return a - b;
}
//此處為獨立函數
function LTrim(str, s) {
if (s == undefined) { s = " "; }
if (str == s && s != " ") { return s; }
var i;
for (i = 0; i
if (str.charAt(i) != s && str.charAt(i) != s) break;
}
str = str.substring(i, str.length);
return str;
}
function RTrim(str, s) {
var i;
if (str == s && s != " ") { return s; }
if (s == undefined) { s = " "; }
for (i = str.length - 1; i >= 0; i--) {
if (str.charAt(i) != s && str.charAt(i) != s) break;
}
str = str.substring(0, i 1);
return str;
}
function Trim(str, s) {
return LTrim(RTrim(str, s), s);
}
///偵測字串是否是由中文,英文,數字以及底線組成的
function chkNickName(str) {
var pattern = /^[wu4e00-u9fa5] $/gi;
if (pattern.test(str)) {
return true;
}
return false;
}
//判斷長度(長度不限為0)
String.prototype.IsLen = function () {
var isRightFormat = false;
var minnum = arguments[0] ? arguments[0] : 0;
var maxnum = arguments[1] ? arguments[1] : 0;
isRightFormat = (minnum == 0 && maxnum == 0 ? true : (calculate_byte(this) >= minnum && calculate_byte(this)
return isRightFormat;
}
//驗證字串是否為字母 數字 _ -
String.prototype.IsStr = function () {
var myReg = /^[0-9a-zA-Z-_] $/;
if (myReg.test(this)) return true;
return false;
}
//驗證使用者名稱
String.prototype.IsUsername = function () {
var myReg = /^[0-9a-zA-Z-_]{3,50}$/;
if (myReg.test(this)) return true;
return false;
}
//驗證密碼
String.prototype.IsPassword = function () {
var myReg = /^[0-9a-zA-Z`~!@#$%^&*()-_ ={}[];:"'?/\]{6,}$/;
if (myReg.test(this)) return true;
return false;
}
//驗證是否為字母
String.prototype.IsEn = function () {
var myReg = /^[a-zA-Z] $/;
if (myReg.test(this)) return true;
return false;
}
//驗證是否為漢字
String.prototype.IsCn = function () {
var myReg = /^[u0391-uFFE5] $/;
if (myReg.test(this)) return true;
return false;
}
//驗證E-mail
String.prototype.IsEmail = function () {
var myReg = /([w-.] )@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)| (([w-] .) ))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)/;
if (myReg.test(this)) return true;
return false;
}
//驗證MSN
String.prototype.IsMSN = function () {
var myReg = /([w-.] )@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)| (([w-] .) ))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)/;
if (myReg.test(this)) return true;
return false;
}
//驗證QQ號碼
String.prototype.IsQQ = function () {
var myReg = /^[1-9]d{4,10}$/;
if (myReg.test(this)) return true;
return false;
}
//驗證網址
String.prototype.IsHttpUrl = function () {
var myReg = /^http://[A-Za-z0-9] .[A-Za-z0-9] [/=?%-&_~`@[]': !]*([^ ""])*$/;
if (myReg.test(this)) return true;
return false;
}
//驗證網域名稱
String.prototype.IsDoMainName = function () {
var myReg = /^[0-9a-zA-Z]([0-9a-zA-Z-] .){1,3}[a-zA-Z]{2,4}?$/;
if (myReg.test(this)) return true;
return false;
}
//驗證IPV4位址
String.prototype.IsIpv4 = function () {
var myReg = /^(2[0-5]{2}|1?[0-9]{1,2}).(2[0-5]{2}|1?[0-9]{1 ,2}).(2[0-5]{2}|1?[0-9]{1,2}).(2[0-5]{2}|1?[0-9]{1 ,2})$/;
if (myReg.test(this)) return true;
return false;
}
//驗證圖片位址(不支援CGI動態產生的圖片)
String.prototype.IsImgURL = function () {
var myReg = /^.(jpeg|jpg|gif|bmp|png|pcx|tiff|tga|lwf)$/;
if (myReg.test(this)) return true;
return false;
}
//驗證手機號碼
String.prototype.IsCellPhone = function () {
var myReg = /^(((d{3}))|(d{3}-))?1[3,5]d{9}$/;
if (myReg.test(this)) return true;
return false;
}
//驗證固定電話
String.prototype.IsPhone = function () {
var myReg = /^[ ]{0,1}(d){1,3}[ ]?([-]?((d)|[ ]){1,12}) $/;
if (myReg.test(this)) return true;
return false;
}
//驗證郵編
String.prototype.IsZipCode = function () {
var myReg = /[0-9]{6}/;
if (myReg.test(this)) return true;
return false;
}
//驗證身分證號碼
String.prototype.IsIdCard = function () {
var myReg = /(^([d]{15}|[d]{18}|[d]{17}[xX]{1})$)/;
if (myReg.test(this)) return true;
return false;
}
//驗證日期格式YY-MM-DD
String.prototype.IsDateFormat = function () {
var myReg = /^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/;
if (myReg.test(this)) return true;
return false;
}
//驗證時間格式HH:MM:SS
String.prototype.IsRangeTime = function () {
var myReg = /^(d{2}):(d{2}):(d{2})$/;
if (myReg.test(this)) return true;
return false;
}
//驗證金額格式
String.prototype.IsMoney = function () {
var myReg = /^[0-9]{1,8}[.]{0,1}[0-9]{0,6}$/;
if (myReg.test(this)) return true;
return false;
}
//字驗證數字格式並判斷數字的圍(min:最小值;max:最大值.)
String.prototype.IsInt = function () {
var isRightFormat = false;
var minnum = arguments[0] ? arguments[0] : 0;
var maxnum = arguments[1] ? arguments[1] : 0;
var myReg = /^[- ]?d $/;
if (myReg.test(this)) {
isRightFormat = (minnum == 0 && maxnum == 0 ? true : (this > minnum && this
}
return isRightFormat;
}
//驗證搜尋關鍵字
String.prototype.IsSearch = function () {
var myReg = /^[|"',.*&@#$;:!^()]/;
if (myReg.test(this)) return false;
return true;
}
//js精確計算字串長度
function calculate_byte(sTargetStr) {
var sTmpStr, sTmpChar;
var nOriginLen = 0;
var nStrLength = 0;
sTmpStr = new String(sTargetStr);
nOriginLen = sTmpStr.length;
for (var i = 0; i
sTmpChar = sTmpStr.charAt(i);
if (escape(sTmpChar).length > 4) {
nStrLength = 2;
} else if (sTmpChar != 'r') {
nStrLength ;
}
}
return nStrLength;
}
//顏色值;
String.prototype.IsColor = function () {
var s = arguments[0] ? arguments[0] : "";
s = s.Trim();
if (s.length != 7) return false;
return s.search(/#[a-fA-F0-9]{6}/) != -1;
}
//js日期重構
Date.prototype.format = 函數(格式){
var o = {
"M ": this.getMonth() 1, //月份
"d ": this.getDate(), //日
"h ": this.getHours(), //小時
"m ": this.getMinutes(), //分鐘
"s ": this.getSeconds(), //第二名
"q ": Math.floor((this.getMonth() 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
}
if (/(y )/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" k ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" o[k]).substr(("" o[k]).length));
}
}
返回格式;
}
函數 HasChinese(value) {
if (/^[u4e00-u9fa5] $/.test(value)) {
返回真實;
}
回傳錯誤;
}
函數 ToDate(dateStr) {
var dStr = dateStr.toString();
dateStr = dStr.replaceAll("-", "/");
return new Date(Date.parse(dateStr));
}
// 是否ID列表
String.prototype.IsIdList = 函數 {
if (s == undefined) {
s = ",";
}
var arr = this.split(s);
for (var i = 0; i
if (isNaN(parseInt(arr[i]))) { 回傳 false; }
}
返回真實;
}
//取得事件觸發的物件
函數 getEventTarget(e) {
e = e || window.event;
回 e.target || e.srcElement;
}
程式碼非常簡潔,簡單,功能卻非常實用,需要的小夥伴參考下