简体   繁体   English

如何使用jquery验证asp.net Web表单中的登录模式?

[英]How to validate a login modal in asp.net webforms using jquery?

I have a simple asp.net webpage which uses a modal bootstrap for login. 我有一个简单的asp.net网页,它使用模态引导程序进行登录。 After the user click on the Login button, it should authenticate the user and download a zip file by calling a server side method "ExportToZip". 用户单击“登录”按钮后,它应通过调用服务器端方法“ ExportToZip”对用户进行身份验证并下载zip文件。 The problem that I am facing is that I can't find a way to first validate the username and password and then to call the server side method "ExportToZip". 我面临的问题是我找不到首先验证用户名和密码,然后调用服务器端方法“ExportToZip”的方法。 All the time it validates the form fields but the server method is called no matter what. 它一直验证表单字段,但无论如何都会调用服务器方法。 And if there is an error in the server method, modal window closes. 如果服务器方法出错,则模态窗口关闭。

I guess I would need to find a way so that the jquery click event triggers first. 我想我需要找到一种方法,以便首先触发jquery click事件。 Then the server side method is triggered to. 然后触发服务器端方法。

Modal window: 模态窗口:

<div class="modal fade" id="LoginModal" tabindex="-1" role="dialog" aria-labelledby="ModalTitle" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                            &times;</button>
                        <h4 class="modal-title" id="ModalTitle">Login with your Syncade credentials</h4>
                    </div>
                    <div class="modal-body">
                        <span id="userNamSpan"></span>
                        <asp:TextBox ID="txtUsername" runat="server" CssClass="form-control" placeholder="Enter Username" AutoCompleteType="Disabled" />
                        <br />
                        <span id="passwordSpan"></span>
                        <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" CssClass="form-control" placeholder="Enter Password" AutoCompleteType="Disabled" />


                    </div>
                    <div class="modal-footer">
                        <asp:Button ID="btnLogin" Text="Login" runat="server" OnClientClick="ExportToZip;" Class="btn btn-primary" />
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>

                    </div>
                </div>
            </div>
        </div>

jquery part: jquery部分:

<script type="text/javascript">
    $(document).ready(function () {
        $("#btnShowLogin").click(function () {
            $('#LoginModal').modal('show');
        });

        //validate form inputs
        $("#btnLogin").click(function (e) {
            if ($("#txtUsername").val() == "")
                $("#userNamSpan").text("Enter Username:");
            else
                $("#userNamSpan").text("");

            if ($("#txtPassword").val() == "")
                $("#passwordSpan").text("Enter Password:");
            else
                $("#passwordSpan").text("");

        });
    });
</script>

Try something like this: 尝试这样的事情:

<asp:Button ID="btnLogin" Text="Login" runat="server" OnClientClick="return ExportToZip();" Class="btn btn-primary" />

function ExportToZip() {
var isValid = true:
if ($("#txtUsername").val() == "")  {
                $("#userNamSpan").text("Enter Username:");
                isValid = false:
     }
        if ($("#txtPassword").val() == "") {
                $("#passwordSpan").text("Enter Password:");
        isValid = false:
   }

   return isValid:
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM