Friday, July 7, 2017

How to add Multiple validation group to the same control ?



In some conditions we may need to validate a controls based on the different conditions consider the below example in which we are validating the same dropdown list from two different validation group Here we are using the  Page_ClientValidate('your validation group name') method which will accept the validation group name as input
  

Example:- 

 
<form id="form1" runat="server">
        <asp:Button Text="First" runat="server" ValidationGroup="firstVG" />
        <asp:Button Text="Second" runat="server" ValidationGroup="secondVG"  OnClientClick="return Check(); " />

        <asp:DropDownList runat="server" ValidationGroup="first" ID="ddl">
            <asp:ListItem Text="--Select--" Value="-1" />
            <asp:ListItem Text="A" />
            <asp:ListItem Text="B" />
        </asp:DropDownList>
        <asp:RequiredFieldValidator ErrorMessage="*" Display="Dynamic" ForeColor="Red" ControlToValidate="ddl" runat="server"  InitialValue="-1" ValidationGroup="firstVG"/>

    </form>

JavaScript File :- 

 

    <script type="text/javascript">
        function Check() {

            var isValid = false;
            isValid = Page_ClientValidate('secondVG');
            if (isValid) {
                isValid = Page_ClientValidate('firstVG');
            }

            return isValid;

        }
    </script>

WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).



After adding the built in validating controls like required field validator in my case I added a new web form and then after adding the required field validator controller in the form the form got compiled but after running we can see the above error on the browser. The solution is that  we need to add  a new key into to the web.config file under the <appSettings> section

In Web.config



  <appSettings>

    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />

  </appSettings>

Git Commands

Git Version   To check the git version Git -v       Git Clone To clone the repository, use the following command: Git clone [u...