String.prototype.myFun = function ()
{
return this = ' *** ' + this + ' ### ';
}
alert( 'some string'.myFun() ); // ' *** some string ### '
var sName = 'User';
sName = sName.myFun();
alert( sName ) ;// ' *** User ### '
//
// Other Many Usefull String Prototypes as bellow
//
String.prototype.Rtrim = function() { return this.replace(/(\s*$)/g, ""); }
String.prototype.Trim = function() { return this.replace(/(^\s*)|(\s*$)/g, ""); }
String.prototype.Left = function(len) { if(isNaN(len)||len==null) { len = this.length; } else { if(parseInt(len)<0||parseInt(len)>this.length) { len = this.length; } } return this.substr(0,len); }
String.prototype.Right = function(len) { if(isNaN(len)||len==null) { len = this.length; } else { if(parseInt(len)<0||parseInt(len)>this.length) { len = this.length; } } return this.substring(this.length-len,this.length); }
String.prototype.Mid = function(start,len) { return this.substr(start,len); }
String.prototype.InStr = function(str) { if(str==null) { str = ""; } return this.indexOf(str); }
String.prototype.InStrRev = function(str) { if(str==null) { str = ""; } return this.lastIndexOf(str); }
String.prototype.LengthW = function() { return this.replace(/[^\x00-\xff]/g,"**").length; }
String.prototype.isIP = function() { var reSpaceCheck = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/; if (reSpaceCheck.test(this)) { this.match(reSpaceCheck); if (RegExp.$1 <= 255 && RegExp.$1 >= 0 && RegExp.$2 <= 255 && RegExp.$2 >= 0 && RegExp.$3 <= 255 && RegExp.$3 >= 0 && RegExp.$4 <= 255 && RegExp.$4 >= 0) { return true; } else { return false; } } else { return false; } }
String.prototype.isLongDate = function() { var r = this.replace(/(^\s*)|(\s*$)/g, "").match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/); if(r==null) { return false; } var d = new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]); return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]&&d.getSeconds()==r[7]); }
String.prototype.isShortDate = function() { var r = this.replace(/(^\s*)|(\s*$)/g, "").match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/); if(r==null) { return false; } var d = new Date(r[1], r[3]-1, r[4]); return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]); }
String.prototype.isDate = function() { return this.isLongDate()||this.isShortDate(); }
String.prototype.isMobile = function() { return /^0{0,1}13[0-9]{9}$/.test(this); }
String.prototype.isEmail = function() { return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(this); }
String.prototype.isZipCode = function() { return /^[\\d]{6}$/.test(this); }
String.prototype.isFileName = function() { return !/[\\\/\*\?\|:"<>]/g.test(this); }
String.prototype.isUrl = function() { return /^http[s]?:\/\/([\w-]+\.)+[\w-]+([\w-./?%&=]*)?$/i.test(this); }
String.prototype.isPhoneCall = function() { return /(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/.test(this); }
String.prototype.isNumeric = function(flag) { if(isNaN(this)) { return false; } switch(flag) { case null: case "": return true; case "+": return /(^\+?|^\d?)\d*\.?\d+$/.test(this); case "-": return /^-\d*\.?\d+$/.test(this); case "i": return /(^-?|^\+?|\d)\d+$/.test(this); case "+i": return /(^\d+$)|(^\+?\d+$)/.test(this); case "-i": return /^[-]\d+$/.test(this); case "f": return /(^-?|^\+?|^\d?)\d*\.\d+$/.test(this); case "+f": return /(^\+?|^\d?)\d*\.\d+$/.test(this); case "-f": return /^[-]\d*\.\d$/.test(this); default: return true; } }
String.prototype.IsColor = function() { var temp = this; if (temp=="") return true; if (temp.length!=7) return false; return (temp.search(/\#[a-fA-F0-9]{6}/) != -1); }
String.prototype.toCase = function() { var tmp = ""; for(var i=0;i<this.length;i++) { if(this.charCodeAt(i)>0&&this.charCodeAt(i)<255) { tmp += String.fromCharCode(this.charCodeAt(i)+65248); } else { tmp += String.fromCharCode(this.charCodeAt(i)); } } return tmp }
String.prototype.toHtmlEncode = function() { var str = this; str=str.replace(/&/g,"&"); str=str.replace(/</g,"<"); str=str.replace(/>/g,">"); str=str.replace(/\'/g,"'"); str=str.replace(/\"/g,"""); str=str.replace(/\n/g,"
"); str=str.replace(/\ /g," "); str=str.replace(/\t/g," "); return str; }
String.prototype.toDate = function() { try { return new Date(this.replace(/-/g, "\/")); } catch(e) { return null; } }
Monday, December 14, 2009
Javascript string.prototype
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment