0

I was trying to do validations before submitting the asp.net form.

However , it keeps getting submitted. Below is code:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        #form1 {
            height: 254px;
            width: 905px;
        }
        .tabClass {
            margin-top: 10%;
        }
        input {
            margin-top: 50px;
        }
        #Name {
            margin-right: 58px;
        }
        #uName {
            margin-right: 55px;
        }
        #p1 {
            margin-right: 66px;
        }
        #Label2 {
            margin-right: 20px;
        }
    </style>
    <script type="text/javascript">
        function DoValidation(parameter) {
            document.getElementById("Button2").click();
            var prompt = "";
            var a = document.getElementById("TextBox_FName").value;
            if (a == "")
                prompt += "Please Enter Full Name. ";
            a = document.getElementById("TextBox_UName").value;
            if (a == "")
                prompt += "Please User Name. ";
            a = document.getElementById("Password1").value;
            if (a == "")
                prompt += "Please Enter Password. ";
            if (a != document.getElementById("Password2").value)
                prompt += "Entered Passwords Don't match. ";

            if (prompt === "") {
                document.getElementById("<%=Button2.ClientID%>").click();
            } else {
                alert(prompt);
                return false;
            }
        }
</script>
</head>
<body>
    <div style="margin-left: 30%;">
        <form id="form1" runat="server">      
        <div>    
            <asp:Label ID="Name" runat="server" Text="Full Name" CssClass="tabClass"></asp:Label>
            <asp:TextBox ID="TextBox_FName"  Height="25px" Width="300px" runat="server" ></asp:TextBox>    
        </div>
        <asp:Label ID="uName" runat="server" Text="User Name" CssClass="tabClass"></asp:Label>
        <asp:TextBox ID="TextBox_UName"  Height="25px" Width="200px" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="p1" runat="server" Text="Password" CssClass="tabClass"> </asp:Label>
        <asp:TextBox ID="Password1" runat="server" Height="25px"  TextMode="Password" Width="200px"></asp:TextBox>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Retype Password"></asp:Label>
        <asp:TextBox ID="Password2" runat="server" Height="25px"  TextMode="Password" Width="200px"></asp:TextBox>
        <p>
           <asp:Button ID="Button3" runat="server"  OnClientClick="return !!DoValidation()"  Text="Register" style="display:none;" />
        </p>
         <asp:Button ID="Button2" runat="server"  OnClick="Button1_Click" Text="HiddenRegister" style="display:none;"/>     

        </form>
        <button onclick="DoValidation()">Reg1 </button>
    </div>
</body>
</html>

My form always gets posted even if i get alert prompt of the "else condition of doValidation() method.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user3440629
  • 198
  • 9

2 Answers2

1

Ah, remove the first line in DoValidation function:

document.getElementById("Button2").click();
Igor
  • 15,833
  • 1
  • 27
  • 32
  • @user3440629 - I have tried your code ,Igor is correct, you should remove this line of code, this is line is causing PostBack. – Rahul Singh Nov 26 '14 at 05:59
1

Try this u r not retruning the function so u have to return function...like below....

<button onclick="return DoValidation()">Reg1 </button>
Sumit Pathak
  • 671
  • 1
  • 6
  • 25