Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the both JavaScript validation and asp.net validation for a control

Tags:

c#

asp.net

c#-4.0

I use a button click with the asp.net validation group and client side validation (javascript). It give the first preference to javascript validation if it returns true means it directly go to the serverside button click event not check the asp.net validations.

<asp:ImageButton ID="img_btn_register" runat="server"
    ImageUrl="~/images/Register1.png"
    **OnClientClick="return validat_form()"** OnClick="img_btn_register_Click1" 
    **ValidationGroup="qa"** />
like image 219
user2176150 Avatar asked Feb 23 '26 03:02

user2176150


1 Answers

Based on your question I understand that- You need your field validations should occur first and then script should execute.

You can call Page_ClientValidate() in your client script to explicitly execute all validations and if it successful then only Client Script should be executed.

Here is a small demo on the same:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script type="text/javascript" language="javascript">
        function ClientScript() { 
            if (Page_ClientValidate("qa"))**// first check the validators in ValidationGroup "qa"**
            {
                alert("Save all Modification?");
            }
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="qa"  runat="server" ControlToValidate="TextBox1"
            Text="*" ErrorMessage="Value in Textbox1 is required!">
        </asp:RequiredFieldValidator>
        <asp:Button ID="Button1" runat="server" ValidationGroup="qa" Text="Test Validation" OnClientClick="ClientScript()" />
    </div>
    </form>
</body>
</html>
like image 107
Ramesh Rajendran Avatar answered Feb 25 '26 16:02

Ramesh Rajendran