News
IBM to offer mobile security as a service IBM will start delivering mobile security-as-a-service (MaaS) through its own cloud infrastructure in India, a move that is expected to better address in-country data requirements... IBM Spectrum Computing: IBM has enlarged its portfolio of software-defined infrastructure solutions with cognitive features for workload management.  * LiFi internet technology has been introduced, the new type of wireless internet connection that gives 100 times faster than traditional WiFi -- Invented by Professor Harald Haas from the University of Edinburgh. * Sci. Rachid Yazami has developed a smart chip that charges smartphones in less than 10 minutes. BenQ has launched BlueCore projector - Consumer electronics major BenQ has launched its first BlueCore laser light source projector. For those unaware, devices with BlueCore laser technology have a high contrast output of 80000:1 with an extended lamp life and efficiency. *** 
  Jan 4 2015 7:53AM     Sara Silva
  0 Comments    2563 Views  
Here Sara Silva gave such a good explanation about login validation Using jQuery Ajax with example code and Demo.
Ajax stands for Asynchronous JavaScript and XML. Ajax is used to Update the part of the web page without reloading the entire page. In other words, Give request to the server and get response with out page reload.
HTML
<body>
    <div class="container login">
        <div class="main">
            <h2>
                Authentication Using JQuery Ajax
            </h2>
            <h5>
                Try Username : thedevelopertips and Password : thedevelopertips</h5>
            <div style="float: left;">
                <span>Enter the UserName:</span> <span>
                    <input id="txtUsername" type="text">
                </span>
            </div>
            <div style="float: left;">
                <span style="float: left; display: block;" id="error1" class="dsNone error"></span>
            </div>
            <br >
            <br >
            <div style="float: left;">
                <span>Enter the Password &nbsp;:</span> <span>
                    <input id="txtPassword" type="text">
                </span>
            </div>
            <div style="float: left;">
                <span style="float: left; display: block;" id="error2" class="dsNone error"></span>
            </div>
            <br >
            <br >
            <div class="ml19px">
                <input type="button" name="password" value="Reset" id="btnReset" class="width55 ml4px btnReset" />
                <input type="button" value="Login" id="btnLoginUsingAjax" class="width55 ml4px btn" />
                <br >
                <br >
                <span style="float: left; padding: 3px;" id="successMsg" class="dsNone success ml19px">
                    Login Success</span> <span style="float: left; padding: 3px;" id="errorMsg" class="dsNone error ml19px">
                        Login Failure</span>
            </div>
        </div>
    </div>
</body>
CSS
<style type="text/css">
        .error
        {
            color: white;
            background-color: rgb(247, 23, 23);
            margin-left: 12px;
            border-radius: 3px;
        }

        .success
        {
            color: white;
            background-color: green;
            margin-left: 12px;
            border-radius: 3px;
        }

        .btn
        {
            line-height: 2.0;
            border-radius: 4px;
            border: none;
            font-size: 14px;
            cursor: pointer;
            color: #fff;
            background-color: #00a9da;
            border-color: #0094bf;
        }

        .btnReset
        {
            line-height: 2.0;
            border-radius: 4px;
            border: none;
            font-size: 14px;
            cursor: pointer;
            color: #fff;
            background-color: #e85f61;
            border-color: #e85f61;
        }

        .width55
        {
            width: 100px;
        }

        .ml4px
        {
            margin-left: 4px;
        }

        .ml19px
        {
            margin-left: 19%;
        }

        .login
        {
            height: 220px;
            background-color: #f5f5f5;
            width: 35%;
            padding: 20px;
            border-radius: 16px;
        }

        .dsNone
        {
            display: none;
        }

    </style>
JavaScript
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#btnLoginUsingAjax").click(function () {
                if ($("#txtUsername").val() == "" || $("#txtUsername").val() == null) {
                    $("#error1").css("display""block");
                    $("#error1").text(" Username ");
                    $("#successMsg").css("display""none");
                    return false;
                }
                else {
                    $("#error1").css("display""none");
                }
                if ($("#txtPassword").val() == "" || $("#txtPassword").val() == null) {
                    $("#error2").css("display""block");
                    $("#error2").text(" Password ");
                    $("#successMsg").css("display""none");
                    return false;
                }
                else {
                    $("#error2").css("display""none");
                }
                $.ajax({
                    type: "POST",
                    url: "UserLogin-Authentication-jQueryAjax.aspx/UserAuthenticate",
                    data: JSON.stringify({ name: $("#txtUsername").val(), pwd: $("#txtPassword").val() }),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    async: true,
                    success: function (data, status) {
                        console.log("CallWM");
                        if (data.d == "success") {
                            $("#successMsg").css("display""block");
                            $("#errorMsg").css("display""none");
                            $("#successMsg").innerHtml = " Login Success ";
                        }
                        else {
                            $("#errorMsg").css("display""block");
                            $("#successMsg").css("display""none");
                            $("#errorMsg").innerHtml = " Login Failure ";
                        }
                    },
                    failure: function (data) {
                        alert(data.d);
                    },
                    error: function (data) {
                        alert(data.d);
                    }
                });
            });
        });
    </script>
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

public partial class UserLogin_Authentication_jQueryAjax : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public static string UserAuthenticate(string name, string pwd)
    {
        string result = string.Empty;
        if (name == "thedevelopertips" && pwd == "thedevelopertips")
        {
            result = "success";
        }
        else
        {
            result = "failure";
        }
        return (result);
    }
}
Demo
Just give Username and Password in the below input field and click submit button. The Given username and password will send to server and authenticate status will be send back to the browser without page reload by jQuery Ajax.
Hi, This is Sara Silva.I hope this page will helps you to validate login page with jQuery Ajax.Thanks.
BackToTop
Comments



 
Search
Recent Posts
Create Amazon ElasticCache Using Memcached in CSharp
Oct 29 2018 12:09PM Posted By Amose
Get Google Map Lat Lng (Geo Point) By Pincode in C#
Oct 28 2018 12:09PM Posted By Pranav
Google URL Shortener in C#
Oct 20 2018 12:09PM Posted By Sanjay
Bind Gridview from CSV file in Asp.Net C#
Oct 5 2018 12:09PM Posted By Michael
Call WebService method from jQuery in every 1 minute
Sep 26 2018 12:09PM Posted By John
Gridview custom CSS in ASP.Net
Sep 14 2018 12:09PM Posted By Micheal Ryan
Read excel file and bind to Gridview in C#
Sep 10 2018 12:09PM Posted By Micheal
Tags
Follow us on Facebook
Follow us on Google +
Recent post in your Email inbox.
Enter your email address: