开发者

validation on userid and password using ajax and asp.net

开发者 https://www.devze.com 2022-12-10 22:37 出处:网络
i want to know how to make validation on userId and password in asp.net application using ajax and jquery because 开发者_如何学Pythoni am new to ajax

i want to know how to make validation on userId and password in asp.net application using ajax and jquery because 开发者_如何学Pythoni am new to ajax any help will be highly apperciated


The first thing would be have a server side script that takes userid and password parameters and verifies if they are valid.

Add Check.ashx generic handler to your project:

<%@ WebHandler Language="C#" Class="Check" %>

using System.Web;

public class Check : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        var userid = context.Request["userid"];
        var password = context.Request["password"];

        string response = IsValid(userid, password) ? "true" : "false";
        context.Response.ContentType = "appliaction/json";
        context.Response.Write("{isvalid:'" + response + "'}");
    }

    private bool IsValid(string userid, string password)
    {
        return (userid == "john" && password == "secret");
    }

    public bool IsReusable
    {
        get { return false; }
    }
}

And then have your page send an ajax query.

Default.aspx:

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(function() {
            $('a.check').click(function() {
                $.ajax({
                    url: '/check.ashx',
                    dataType: 'json',
                    data: {
                        userid: $('input[name=userid]').val(),
                        password: $('input[name=password]').val()
                    },
                    success: function(json) {
                        alert(json.isvalid);
                    }
                });
                return false;
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        userid: <input type="text" name="userid" value="" />
        password: <input type="text" name="password" value="" />
        <a href="#" class="check">Check</a>
    </div>
    </form>
</body>
</html>


well, ajax is basically the ability of calling webservices/wcf from the client (javascript), and you execute some webmethod with parameters, and receive some data, and that's kind off it

also you should not rely just on client validation because, the user can just disable javascript from his browser and all the ajax stuff/ javascript is not going to work


Funny you should ask, I started this Blog post last night, no kidding!!!

http://professionalaspnet.com/archive/2009/11/11/Validating-A-Username-Using-JQuery-and-ASP.NET-Membership-Provider.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消