使用jQuery中的$.ajax進行即時驗證

來源:互聯網
上載者:User

使用jQuery和一般處理常式即時驗證使用者錄入的學號是否重複,當游標離開輸入框即給出提示。

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

<!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>
    <style type="text/css">
        .clsShow
        {
            font-size: 13px;
            border: solid 1px #cc3300;
            padding: 2px;
            display: none;
            margin-bottom: 5px;
            background-color: #ffe0a3;
        }

    </style>
    <script type="text/javascript" src="Scripts/jquery-1.4.2.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#btnSave").click(function () {
                if ($(".clsShow").html().toString() != "")//存在提示資訊,則不允許提交表單
                    return false;
                else
                    return true;
            });
            $("#txtNum").focus(); //輸入焦點
            $("#txtNum").keydown(function (event) {
                if (event.which == "13") {//斷行符號鍵,移動游標到密碼框
                    $("#txtName").focus();
                    $("#txtNum").trigger("blur");
                }
            });
            $("#txtNum").blur(function () {
                //擷取學號
                var strTxtName = encodeURI($("#txtNum").val());
                //開始發送資料
                $.ajax
                ({ //請求驗證學號是否重複
                    url: "Check.ashx", 
                    type: "post",
                    //傳送請求資料
                    data: { txtNum: strTxtName },
                    success: function (strValue) { //登入成功後返回的資料
                        //根據傳回值進行狀態顯示
                        if (strValue == "True") {//注意是True,不是true
                            $(".clsShow").css("display", "inline");
                            $(".clsShow").html("學號已存在,請修改!");
                        }
                        else {
                            $(".clsShow").hide(); //就是把display屬性變成none
                            $(".clsShow").html("");
                        }
                    }
                })
            })
        })
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        學號:<asp:TextBox
            ID="txtNum" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
            ControlToValidate="txtNum" ErrorMessage="不可為空"></asp:RequiredFieldValidator>
        <div class="clsShow"></div>
        <br />
        姓名:<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
            ControlToValidate="txtName" ErrorMessage="不可為空"></asp:RequiredFieldValidator>
        <br />
        數學:<asp:TextBox
            ID="txtMath" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
            ControlToValidate="txtMath" ErrorMessage="不可為空"></asp:RequiredFieldValidator>
        <asp:RangeValidator ID="RangeValidator1" runat="server"
            ControlToValidate="txtMath" ErrorMessage="分數在0-100之間" MaximumValue="100"
            MinimumValue="0" Type="Integer"></asp:RangeValidator>
        <br />
        英語:<asp:TextBox ID="txtEnglish" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
            ControlToValidate="txtEnglish" ErrorMessage="不可為空"></asp:RequiredFieldValidator>
        <asp:RangeValidator ID="RangeValidator2" runat="server"
            ControlToValidate="txtEnglish" ErrorMessage="分數在0-100之間" MaximumValue="100"
            MinimumValue="0" Type="Integer"></asp:RangeValidator>
        <br />
        語文:<asp:TextBox ID="txtChinese" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
            ControlToValidate="txtChinese" ErrorMessage="不可為空"></asp:RequiredFieldValidator>
        <asp:RangeValidator ID="RangeValidator3" runat="server"
            ControlToValidate="txtChinese" ErrorMessage="分數在0-100之間" MaximumValue="100"
            MinimumValue="0" Type="Integer"></asp:RangeValidator>
        <br />
        <asp:Button ID="btnSave" runat="server" Text="儲存" onclick="btnSave_Click" />
        <asp:Button ID="btnBack" runat="server" Text="返回" CausesValidation="False"
            onclick="btnBack_Click" />
        <asp:Label ID="lblMsg" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>

一般處理常式Check.ashx代碼:

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

using System;
using System.Web;

public class Check : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string num = context.Request["txtNum"].ToString();
        bool result = false;
        if(num=="12")//為了簡化代碼,沒有訪問資料庫。實際項目應查詢資料庫。
        {
            result = true;
        }
        context.Response.Write(result);
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.