jquery 验证

---前台代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
    <script src="jQuery.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
   
   $(document).ready(function(){
    $("#txtName").keyup(function(){
        if($("txtName").val() =="")
        {
            alert("姓名不能为看空");
            $("txtName").focus();
            return false;
        }
        else
        {
          $.ajax({
                type:"get",
                url:"jSencend.aspx",
                data:{userName:$("#txtName").val()},
                beforeSend:function(){$("#spa1").html("logining");},
                error:function(){alert("错误");},           
                success:function(data){$("#spa1").html(decodeURI(data));}           
             });
            return true;
        }
       
        });
    });

    </script>

</head>
<body>
    <form id="form1" runat="server">
        <div>

            <input id="txtName" type="text"/>       
             <input id="txtPass" type="text"/>  
            <span id="spa1"></span>
        </div>
    </form>
</body>
</html>


--后台代码

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

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

    }
}


--提交到窗体的前台代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="jSencend.aspx.cs" Inherits="Js_jSencend" %>

<!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>
</head>
<body>
    <form id="form1" action="" runat="server">
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="Secend() " />
    </form>
</body>
</html>



---提交到窗体的后台代码

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Js_jSencend : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string retVal = "";
            //string sNamePost = Request.Form["userName"];    //post
            string sName = Request.Params["userName"];      //get
            //此处sName的值写死了,如果验证sName在数据库中是否存在,可以换成从数据库中取出
            if (sName == "ike")
            {
                retVal = "Login is success";
            }
            else
            {
                retVal = "Login is false";
            }
            Response.Write(retVal);
            Response.End();
        }

    }
}


完整的代码,代码直接复制就可以用,记得下载jquery.js文件
posted @ 2009-07-23 11:02  艾伦  阅读(295)  评论(0编辑  收藏  举报