Validate page using JavaScript Page_ClientValidate function in ASP.Net

Last Reply 3 months ago By dharmendr

Posted 3 months ago

hi

see below code:

<div id="DPayam" runat="server" class="DPayam">
    <asp:Label ID="Label33" runat="server" CssClass="lblmalek">plese click below button</asp:Label>
</div>
<div id="message" class="atten">
    <asp:Label ID="Label38" runat="server" CssClass="lblatten">please enter name</asp:Label>
</div>
<asp:RequiredFieldValidator Display="Dynamic" ID="RequiredFieldValidator1" runat="server" ControlToValidate="Txtname"></asp:RequiredFieldValidator>
<script type="text/javascript">
    function Validate() {
        var message = document.getElementById("message");
        var DPayam = document.getElementById("DPayam");
        if (Page_ClientValidate()) {
            message.style.display = "none";
            DPayam.style.display = "block";
        }
        else {
            DPayam.style.display = "none";
            message.style.display = "block";
        }
    }
</script>

here I define 2 div

when run page first div show "please click on below button" and here is textbox that users should enter text on this text box if they don't enter text it will show error "please enter name" <div id=""message">

here in script  I define  that when click button if users don't type name it will :

    <script type="text/javascript">
        function Validate() {
            var message = document.getElementById("message");
            var DPayam = document.getElementById("DPayam");
            if (Page_ClientValidate()) {
                message.style.display = "none";
                DPayam.style.display = "block";
            }
            else {
                DPayam.style.display = "none";
                message.style.display = "block";
            }
        }
    </script>

but here when I don't enter name when click butoon it doesn't do any thing I mean here I expect that it will Dpayam= dispaly none and display  div id message..

and below are css:

.DPayam {
        float:right;
   display:normal;
}
.atten {
    float: right;
    direction: rtl;
    display: none;
    color: red;
    font-size: 14.5px;
    width: 350px;
    margin: 30px 0px 0 0;
}

best regards

 neda

Posted 3 months ago Modified on 2 months ago
nedash says:
<script type="text/javascript">
      function Validate() {
          var message = document.getElementById("message");
          var DPayam = document.getElementById("DPayam");
          if (Page_ClientValidate()) {
              message.style.display = "none";
              DPayam.style.display = "block";
          }
          else {
              DPayam.style.display = "none";
              message.style.display = "block";
          }
      }
  </script>

 Change the above with the below code.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .DPayam
        {
            float: right;
            display: normal;
        }
        .atten
        {
            float: right;
            direction: rtl;
            display: none;
            color: red;
            font-size: 14.5px;
            width: 350px;
            margin: 30px 0px 0 0;
        }
    </style>
    <script type="text/javascript">
        function Validate() {
            var message = document.getElementById("message");
            var DPayam = document.getElementById("DPayam");
            if (Page_ClientValidate()) {
                message.style.display = "none";
                DPayam.style.display = "block";
            }
            else {
                DPayam.style.display = "block";
                message.style.display = "block";
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:TextBox runat="server" ID="Txtname" CssClass="DPayam" />
    <asp:RequiredFieldValidator Display="Dynamic" ID="RequiredFieldValidator1" runat="server"
        ControlToValidate="Txtname"></asp:RequiredFieldValidator>
    <div id="DPayam" runat="server" class="DPayam">
        <asp:Label ID="Label33" runat="server" CssClass="lblmalek">plese click below button</asp:Label>
    </div>
    <div id="message" class="atten">
        <asp:Label ID="Label38" runat="server" CssClass="lblatten">please enter name</asp:Label>
    </div>
    <asp:Button CssClass="DPayam" Text="Validate" runat="server" OnClientClick="Validate();" />
    </form>
</body>
</html>

Screenshot


Posted 3 months ago Modified on 3 months ago

Change the DPayam.style.display = "block"; to none in else condition.

If it doesnt help you out then please provide screenshot of your requirement.

 

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html