Validating jQuery in ASP.NET

I have a strange situation, maybe this is a simple fix or something that I can miss, but here's the question.

I have an asp.net form with a main page and my validation works without problems, but problems arise when I try to connect my click event to the server side,

here is what I meant: I have a form with several fields on it, and if the form is empty, if it should be submitted STOP, otherwise let me execute the server side of the script

but it doesn’t happen, even my form is in an invalid state (I get an error message indicating that I need to enter the necessary files), but still I execute my server side of the script.

I would like to execute my server side script only if the form is in the correct state.

here is my code: my main page

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<!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>jQuery Validation in ASP.NET Master Page</title>
    <script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.3.2-vsdoc2.js" type="text/javascript"></script>
    <script src="Scripts/jquery.validate.js" type="text/javascript"></script>

    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

my content page

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
      <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <script type="text/javascript">
        $(document).ready(function() {
            $("#aspnetForm").validate({
                rules: {
                    <%=txtName.UniqueID %>: {
                        minlength: 2,
                        required: true
                    },
                     <%=txtEmail.UniqueID %>: {                        
                        required: true,
                        email:true
                    }
                }, messages: {
                    <%=txtName.UniqueID %>:{ 
                        required: "* Required Field *", 
                        minlength: "* Please enter atleast 2 characters *" 
                    }
                }
            });
        });
    </script>

    Name: <asp:TextBox ID="txtName" MaxLength="30" runat="server" /><br />
    Email: <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br />
    <asp:Button ID="btnSubmit" runat="server" onclick="SubmitTheForm();" Text="Submit" />
</asp:Content>



function SubmitTheForm() {
    SaveTheForm();
}


function SaveTheForm() {
    debugger;
    var request = buildNewContactRequest();

    ContactServiceProxy.invoke({ serviceMethod: "PostNewContact",
        data: { request: request },
        callback: function(response) {     
            processCompletedContactStore(response);
        },
        error: function(xhr, errorMsg, thrown) {
            postErrorAndUnBlockUI(xhr, errorMsg, thrown);
        }
    });  
    return false; 
} 

I tried both ways

1)

$(document).ready(function() {
        $("#btnSubmit").click(function(event) {
            event.preventDefault(); 
            SaveTheForm();
    });
});

2)

<asp:Button ID="btnSubmit" runat="server" onclick="SubmitTheForm();

  function SubmitTheForm() {
        SaveTheForm();
    }
+3
source share
4 answers

You publish your data without checking if the form is valid or not. I think you should use something like this

function SubmitTheForm() {
    if ($("#aspnetForm").valid()) SaveTheForm();
}
+4
source

Sorry, I give you bad advice. Forget my previous post. What is the SubmitTheForm () method? You are using the jQuery validation plug-in, so you just need to attach the validation material to the form, and everything should be correct with the world.

0
source

, onclick

return SaveTheForm();

:

<asp:Button ID="btnSubmit" runat="server" onclick="return SubmitTheForm();" Text="Submit" />
0

jQuery script Head ContentPlaceHolder

0

Source: https://habr.com/ru/post/1745834/


All Articles