Monday, 22 April 2013

Get querystring in javascript


function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.search);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

set and get cookies in javascript/C#

Using Javascript

function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value + "; path=/";

}

/*Get Cookie Value */
function getCookie(c_name) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) {
            return unescape(y);
        }
    }
}

From .cs file

HttpCookie oCookie = (HttpCookie)HttpContext.Current.Request.Cookies["Cookiename"];
oCookie.Expires = DateTime.Now.AddHours(4);
oCookie.Value="123";
HttpContext.Current.Response.Cookies.Add(oCookie);

check browser in jquery


navigator.sayswho = (function () {
    var N = navigator.appName,
        ua = navigator.userAgent,
        tem;
    var M = ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
    if (M && (tem = ua.match(/version\/([\.\d]+)/i)) != null) M[2] = tem[1];
    M = M ? [M[1], M[2]] : [N, navigator.appVersion, '-?'];
    return M;
})();

Friday, 22 March 2013

Fix Div on top of window with windows scroll in html



// This function will be executed when the user scrolls the page.
$(window).scroll(function(e) {
    // Get the position of the location where the scroller starts.
    var scroller_anchor = $(".scroller_anchor").offset().top;
   
    // Check if the user has scrolled and the current position is after the scroller start location and if its not already fixed at the top
    if ($(this).scrollTop() >= scroller_anchor && $('.scroller').css('position') != 'fixed')
    {    // Change the CSS of the scroller to hilight it and fix it at the top of the screen.
        $('.scroller').css({
            'background': '#CCC',
            'border': '1px solid #000',
            'position': 'fixed',
            'top': '0px'
        });
        // Changing the height of the scroller anchor to that of scroller so that there is no change in the overall height of the page.
        $('.scroller_anchor').css('height', '50px');
    }
    else if ($(this).scrollTop() < scroller_anchor && $('.scroller').css('position') != 'relative')
    {    // If the user has scrolled back to the location above the scroller anchor place it back into the content.
       
        // Change the height of the scroller anchor to 0 and now we will be adding the scroller back to the content.
        $('.scroller_anchor').css('height', '0px');
       
        // Change the CSS and put it back to its original position.
        $('.scroller').css({
            'background': '#FFF',
            'border': '1px solid #CCC',
            'position': 'relative'
        });
    }
});


CSS:-----------------------------------------------
.scroller_anchor{height:0px; margin:0; padding:0;}
.scroller{background:#FFF; border:1px solid #CCC; margin:0 0 10px; z-index:100; height:50px; font-size:18px; font-weight:bold; text-align:center; width:960px;}




 
    <!-- This div is used to indicate the original position of the scrollable fixed div. -->
    <div class="scroller_anchor"></div>
   
    <!-- This div will be displayed as fixed bar at the top of the page, when user scrolls -->
    <div class="scroller">This is the scrollable bar</div>
   
 
<div style="height:1400px"></div>

Thursday, 13 September 2012

Selecting / Deselecting all the CheckBoxes Inside a GridView

Use this javascript function in gridview for select and deselect checkbox

function chkType(chk, grd) {
    var mainChk;

    // alert (grd);
    //   alert(document.getElementById("ctl00_ContentPlaceHolder1_"+grd+"_ctl01_"+chk));

    if (document.getElementById("ctl00_ContentPlaceHolder1_" + grd + "_ctl01_" + chk) == null) {
        mainChk = document.getElementById("ctl00_ContentPlaceHolder1_" + grd + "_ctl02_" + chk);
    }
    else {
        mainChk = document.getElementById("ctl00_ContentPlaceHolder1_" + grd + "_ctl01_" + chk);

    }
    //if(document.getElementById("ctl00_ContentPlaceHolder1_"+grd+"_ctl01_"+chk).checked==true)
    if (mainChk.checked == true) {

        SelectAll(grd);
    }
    else {
        Deselect(grd);
        //             var id=document.getElementById("ctl00_ContentPlaceHolder1_GrdAccountType_ctl01_chkAll");
        //             id.checked = false;
    }

}


function SelectAll(Grd) {
    var i;

    var count = document.getElementById("ctl00_ContentPlaceHolder1_" + Grd).rows.length;


    for (i = 2; i <= count; i++) {
        if (i < 10) { var id = "ctl00_ContentPlaceHolder1_" + Grd + "_ctl0" + i + "_ChkSelect"; }
        else { var id = "ctl00_ContentPlaceHolder1_" + Grd + "_ctl" + i + "_ChkSelect"; }
        var elem = document.getElementById(id);
        try {
            elem.checked = true;
            ;
        }
        catch (err) { }
    }
    var mainChk;
    if (document.getElementById("ctl00_ContentPlaceHolder1_" + Grd + "_ctl01_chkAll") == null) {
        mainChk = document.getElementById("ctl00_ContentPlaceHolder1_" + Grd + "_ctl02_chkAll");
    }
    else {
        mainChk = document.getElementById("ctl00_ContentPlaceHolder1_" + Grd + "_ctl01_chkAll");
    }
    mainChk.checked = true;

}

function Deselect(Grd) {

    var i;
    var count = document.getElementById("ctl00_ContentPlaceHolder1_" + Grd).rows.length;
    for (i = 2; i <= count; i++) {
        if (i < 10) {
            var id = "ctl00_ContentPlaceHolder1_" + Grd + "_ctl0" + i + "_ChkSelect";

        }
        else {
            var id = "ctl00_ContentPlaceHolder1_" + Grd + "_ctl" + i + "_ChkSelect";
        }
        try {
            var elem = document.getElementById(id);
            elem.checked = false;
        }
        catch (err) {

        }

    }

    var mainChk;
    if (document.getElementById("ctl00_ContentPlaceHolder1_" + Grd + "_ctl01_chkAll") == null) {
        mainChk = document.getElementById("ctl00_ContentPlaceHolder1_" + Grd + "_ctl02_chkAll");
    }
    else {
        mainChk = document.getElementById("ctl00_ContentPlaceHolder1_" + Grd + "_ctl01_chkAll");
    }
    //if(document.getElementById("ctl00_ContentPlaceHolder1_"+grd+"_ctl01_"+chk).checked==true)
    mainChk.checked = false;
}

Maintaining State of CheckBoxes While Paging in a GridView Control

 Here I User server side events for maintain checkbox state.


protected void gvdetails_PageIndexChanging(object sender, GridViewPageEventArgs e)

{

SaveCheckedValues();

gvdetails.PageIndex = e.NewPageIndex;

BindGridData();

PopulateCheckedValues();

}

//This method is used to populate the saved checkbox values

private void PopulateCheckedValues()

{

ArrayList userdetails = (ArrayList)Session["CHECKED_ITEMS"];

if (userdetails != null && userdetails.Count > 0)

{

foreach (GridViewRow gvrow in gvdetails.Rows)

{

int index = (int)gvdetails.DataKeys[gvrow.RowIndex].Value;

if (userdetails.Contains(index))

{

CheckBox myCheckBox = (CheckBox)gvrow.FindControl("chkSelect");

myCheckBox.Checked = true;

}

}

}

}

//This method is used to save the checkedstate of values

private void SaveCheckedValues()

{

ArrayList userdetails = new ArrayList();

int index = -1;

foreach (GridViewRow gvrow in gvdetails.Rows)

{

index = (int)gvdetails.DataKeys[gvrow.RowIndex].Value;

bool result = ((CheckBox)gvrow.FindControl("chkSelect")).Checked;

// Check in the Session

if (Session["CHECKED_ITEMS"] != null)

userdetails = (ArrayList)Session["CHECKED_ITEMS"];

if (result)

{

if (!userdetails.Contains(index))

userdetails.Add(index);

}

else

userdetails.Remove(index);

}

if (userdetails != null && userdetails.Count > 0)

Session["CHECKED_ITEMS"] = userdetails;

}

Email Validation in jquery

Method :1
  string MatchEmailPattern =
   @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
     + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
    [0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
     + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
    [0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
     + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";

  public static bool IsEmail(string email)
  {
     if (email != null) return Regex.IsMatch(email, MatchEmailPattern);
     else return false;
  }


Client Side:

 var emailPat = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
                    var matchArray = EmailId.match(emailPat);
                    if (matchArray == null) {
                        $("#txtEmail").css("border", "1px solid Red");
                        $("#txtEmail").val('Invalid Email Address');
                        $("#txtEmail").focus();
                        return false;
                    }



Method :2


function validateEmail(sEmail) {
    var reEmail = /^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/;

    if (!sEmail.match(reEmail)) {
        return false;
    }

    return true;

}