Showing posts with label JQuery. Show all posts
Showing posts with label JQuery. Show all posts

Wednesday, 2 January 2013

JQuery Input text box Numeric Validation Integer and Decimal



///////////////////////////////////////IntegerValidation ///////////////////////////////////////////////////////
            $("#txtRequestedDays").keyup(function (e) {
                if (/\D/g.test(this.value)) {
                    // Filter non-digits from input value.
                    this.value = this.value.replace(/\D/g, '');
                }
            });


///////////////////////////////////////Decimal Validation ///////////////////////////////////////////////////////
            $("#txtRequestedDays").keyup(function () {
                if (this.value != this.value.replace(/[^0-9\.]/g, '')) {
                    this.value = this.value.replace(/[^0-9\.]/g, '');
                }
            });


Monday, 31 December 2012

Resize or Reduce Dialog of jQuery UI DatePicker plugin using CSS

Dear all
Here is a new problem that all of us can we fix and after some time we forget how to do this again so i would  like to share it with all of you
which is how to increase or decrease the JQuery date picker dialog size thorough JQuery CSS class or our CSS class by the code below as it is

In our class

<style type = "text/css">
.ui-datepicker { font-size:9pt !important}
</style>
 
Or in the jQuery UI Calendar CSS file
.ui-datepicker { font-size:9pt !important}

Sunday, 9 September 2012

JQuery Loading animation in postback

Hi
I want to demonstrate in this article is how to make our web site is looking more attractive by putting an animation while loading the page in post back.

first we can create and design the loading animation from this website Here.

1- First we will create a simple button and do not forget to add the JQUERY reference classes like this.


<script src="Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.7.2.js" type="text/javascript"></script>
<script src="Scripts/jquery.effects.core.js" type="text/javascript"></script>


2- put a simple button 
 <asp:Button ID="btnSearch" runat="server" Text="Search"  />

3-After creating and downloading the animation as a .gif image put it like this.


<div style="text-align: center; vertical-align: middle; font-family: Verdana; color: Blue;
                        position: absolute; top: 50%; left: 50%; margin-left: -88px; font-size: small;
                        background-color: White; border:2px solid #f90;" id="dvProgress" runat="server">
                        Please Wait ...
                        <img src="images/ajax-loader.gif" id="loader" style="vertical-align: middle;" alt="Processing" />
                    </div>

4-Put the below JQUERY code.


 <script type="text/javascript">


$(document).ready(function () {

    $('#dvProgress').hide();  // This is to hide the animation progress at first time


       $("#btnSearch").click(function () {     // show the animation progress upon we click the search button
       $("#dvProgress").show();      
   });   
      });
    </script>

Note: as we know that how is the IE is sucks you will find in all other browsers that the animation running perfectly except the IE so put this extra line of  code like this. 


        $("#dvProgress").show();
        setTimeout('document.images["loader"].src = "images/ajax-loader.gif"', 200);  



Happy Programming :)


Wednesday, 15 August 2012

Microsoft JScript runtime error: 'jQuery' is undefined

In  some cases  as we know the silly internet explorer gives us a weird errors as the tittle of this post, so after searching a lot of time please make sure while you adding the JQuery References class to put it like this (~/).


 <script src="~/Scripts/jquery-ui-1.8.20.custom.min.js" type="text/javascript"></script>
 <script src="~/Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
 <script src="~/Scripts/jquery-1.7.2.js" type="text/javascript"></script> 

Instead of this

 <script src="Scripts/jquery-1.7.2.js" type="text/javascript"></script>

This error may appear or may be not just make sure to prevent any wasting time in the future 

Wednesday, 18 July 2012

JQuery Autocomplete with display member and value member

This is a the second article for the JQuery auto complete text box which demonstrates how to create an autocomplete text box which retrieve value and key or by other words value member and display member,

in our previous article we know how we can retrieve data from Data Base and fill it into the autocomplete like here .

but in most scenarios this is not satisfied for us unless we get the two dimensions or fields.

Example: we need to create a autocomplete text box which can search by employee name and after selecting the employee name we can get his ID. 


First our method inside the aspx page to get the data      



 [WebMethod]
        [ScriptMethod]
        public static ArrayList GetAutoCompleteData(string EmployeeName)
        {
            List<Employee> result = new List<Employee>();
            List<string> result2 = new List<string>();
            ArrayList myArr = new ArrayList();
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            using (SqlConnection con = new SqlConnection("Data Source=sv-intranet04;Initial Catalog=HR_DEV;Persist Security Info=True;User ID=cpr1;Password=cpr1"))
            {
                using (SqlCommand cmd = new SqlCommand("select DISTINCT FullName_E as EmployeeName,EmployeeID as ID from EmployeeProfile_view1 where FullName_E LIKE '%" + EmployeeName + "%'", con))
                {
                    con.Open();

                    SqlDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {

                        Employee newEmp = new Employee();
                     
                        newEmp.EmployeeName = dr["EmployeeName"].ToString();
                        newEmp.EmployeeID = dr["ID"].ToString();
                        newEmp.ID = dr["ID"].ToString();
                   
                        myArr.Add(newEmp);
                     
                    }
                    string str = serializer.Serialize(result);
                     return myArr;               
                }
            }
        }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Second our Emplyee Class



 [DataContract()]
    public class Employee
    {
        [DataMember]
        public string ID;

        [DataMember]
        public string EmployeeID;

        [DataMember]
        public string EmployeeName;

    }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Third our JQuery code


$(function () {

 SearchText();


});


function SearchText() {
    $("#autosuggest").autocomplete({
        source: function (request, response) {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "MyTest.aspx/GetAutoCompleteData",
                data: "{'EmployeeName':'" + document.getElementById('autosuggest').value + "'}",
                dataType: "json",
                success: function (data) {
                    // response(data.d);
                    response($.map(data.d, function (item) {
                        return {
                            label: item.EmployeeName,
                            val: item.ID
                        }
                    }))

                },
                error: function (XMLHttpRequest, callStatus, errorThrown) { alert(callStatus); }
            });
        },
        select: function (event, ui) { $("#Result").text(ui.item.val); }
    });    

}



Wednesday, 11 July 2012

JQuery autocompete from DataBase through JSON

Finally i found the simplest way to fill autocomplete text box by getting a list of employees as an example from Database, Using a normal web method will type it inside the aspx page.

bellow is how to do this


  • first the aspx page

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
    <link href="css/jquery.ui.all.css" rel="stylesheet" type="text/css" />
    <link href="css/jquery.ui.theme.css" rel="stylesheet" type="text/css" />
    <link href="css/jquery.ui.base.css" rel="stylesheet" type="text/css" />
    <link href="css/jquery.ui.autocomplete.css" rel="stylesheet" type="text/css" />
    <script src="Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.7.2.js" type="text/javascript"></script>
    <script src="Scripts/jquery-ui-1.8.20.custom.min.js" type="text/javascript"></script>
    <title>hiiiiiiiiiiiiiii</title>
    <script type="text/javascript">
        $(document).ready(function () {
            SearchText();
        });
        function SearchText() {
            $("#autosuggest").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "MyTest2.aspx/GetAutoCompleteData",
                        data: "{'username':'" + document.getElementById('autosuggest').value + "'}",
                        dataType: "json",
                        success: function (data) {
                            response(data.d);
                        },
                        error: function (result) {
                            alert("Error");
                        }
                    });
                }
            });
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="autosuggest" runat="server"></asp:TextBox>
    </div>
 
    </form>
</body>
</html>



  • Second the code behind (Put this method inside your aspx page)
 [WebMethod]
        public static List<string> GetAutoCompleteData(string username)
        {
            List<string> result = new List<string>();
            using (SqlConnection con = new SqlConnection("Data Source=sv-intranet04;Initial Catalog=HR_DEV;Persist Security Info=True;User ID=cpr1;Password=cpr1"))
            {
                using (SqlCommand cmd = new SqlCommand("select DISTINCT FullName_E from EmployeeProfile_view1 where FullName_E LIKE '%" + username + "%'", con))
                {
                    con.Open();
                  //  cmd.Parameters.AddWithValue("@SearchText", username);
                    SqlDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        result.Add(dr["FullName_E"].ToString());
                    }
                    return result;
                }
            }
        }

  • Happy Codding :)

Tuesday, 13 March 2012

JQuery methods dose not work after calling any ajax event

After doing any event from any ajax controls i found that the JQuery code dose not work this is happening because of any Ajax event is removeing the scripts which is loaded at first time when we open the page,  so we need to load again each time the functions of the JQuery after each Ajax event.

Here below an example for what i did to fix these problem



$(document).ready(function () {

    EndRequestHandler();

});

function load() {
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
}

function EndRequestHandler() {

    $("#pHeader1").click(function () {
        $("#pBody1").slideToggle("slow");
    });


    $("#pHeader2").click(function () {
        $("#pBody2").slideToggle("slow");
    });

    $("#pHeader3").click(function () {
        $("#pBody3").slideToggle("slow");
    });
}

EndRequestHandler function is a function include inside it all the JQuerys calls and the function Load() where we add all the JQuery code again after firing any ajax event