Tuesday, 3 March 2015

JQery helper methods

In this post I am gonna give you some of the JQuery helper methods which I am using in my application frequently. Below are some of them , with explanation:

Usage:

Copy all the codes given at the end of this post to a js  file and add a reference of it to your page.
Or Click here to download the file


1. Remove Special Characters from a string:
var str="Hello%how%are$you";
str.removeSplChars();
output: Hellohowareyou

2. Trim from left:
var str="%hello";
str.trimStart('%');
Output: hello

3. Trim from right:
var str="hello%";
str.trimEnd('%');
Output: hello

4. String Contains:
var str="welcome to SPShell. The SharePoint blog";
str.contains("SharePoint");
Output: true

5. IsUndefined.
var undefinedVar;
IsUndefined(undefinedVar);
Output: true

6. isIEBrowser (check is browser is IE)

isIEBrowser();
Output: false (my browser is chrome when I excecuted this code) .

7. checkChrome
checkChrome();
 Output: true(my browser is chrome when I excecuted this code) .

8. getQueryStrings

Now here my URL was: http://stackoverflow.com/search?q=sharepoint&state=active&live=true
getQueryStrings();
Output : (a javascript object)
                {q: "sharepoint", state: "active", live: "true"}

9. IsValidEmail
IsValidEmail("vikas@live.com");
 Output: true.

Here is the Source Code (js):
You can also download this code here.
String.prototype.removeSplChars = function () {
    return this.replace(/[^a-zA-Z0-9 ]/g, "");
};
String.prototype.trimStart = function (c) {
    if (this.length == 0)
        return this;
    c = c ? c : ' ';
    var i = 0;
    var val = 0;
    for (; this.charAt(i) == c && i < this.length; i++);
    return this.substring(i);
}
String.prototype.trimEnd = function (c) {
    c = c ? c : ' ';
    var i = this.length - 1;
    for (; i >= 0 && this.charAt(i) == c; i--);
    return this.substring(0, i + 1);
}
String.prototype.contains = function(it) { return this.indexOf(it) != -1; };
function IsUndefined(value) {
    if (typeof value == 'undefined') {
        return true;
    }
    else if (value == null) {
        return true;
    }
    else return false;
}
function getInternetExplorerVersion() {
    var rv = -1;
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    else if (navigator.appName == 'Netscape') {
        var ua = navigator.userAgent;
        var re = new RegExp("Trident/.*rv:([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}
function isIEBrowser() {
    if (getInternetExplorerVersion() > -1) {
        return true;
    }
    else return false;
}
function checkChrome() {
    var isChromium = window.chrome,
    vendorName = window.navigator.vendor;
    if (isChromium !== null && isChromium !== undefined && vendorName === "Google Inc.")
        return true;    // is chrome
    else       // not  chrome
        return false;
}
function getQueryStrings() {
    var assoc = {};
    var decode = function (s) { return decodeURIComponent(s.replace(/\+/g, " ")); };
    var queryString = location.search.substring(1);
    var keyValues = queryString.split('&');
    for (var i in keyValues) {
        var key = keyValues[i].split('=');
        if (key.length > 1) {
            assoc[decode(key[0])] = decode(key[1]);
        }
    }
    return assoc;
}
function IsValidEmail(email) {
    if (!IsUndefined(email)) {
        var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        return regex.test(email);
    }
    else return false;
}
Hope it helped you :)