How to: Implement Custom Date Validation in Batch Edit Mode

  • 5 min to read

This example illustrates how to implement custom date validation in Batch Edit mode:

1) Use the BatchEditCardValidating event to check values on the client.

2) Use the CardValidating event to check values on the server.

3) Use the AllowValidationOnEndEdit property to switch between validation modes on the client side. If the variation between the HireDate and BirthDate columns is less than 18, input data is considered invalid and data update is not allowed.

<script type="text/javascript">
    function OnValidation(s, e) {
        if (!clientChkb.GetChecked()) {
            return;
        }
        var grid = ASPxClientCardView.Cast(s);
        var cellInfo1 = e.validationInfo[grid.GetColumnByField("BirthDate").index];
        var cellInfo2 = e.validationInfo[grid.GetColumnByField("HireDate").index];
        var years = CheckYears(cellInfo1.value, cellInfo2.value);
        if (years == null || years < 18) {
            cellInfo1.isValid = false;
            cellInfo2.isValid = false;
            cellInfo2.errorText = "Invalid difference between Hire Date and Birth Date";
            cellInfo1.errorText = "Invalid difference between Hire Date and Birth Date";
        } else {
            cellInfo1.isValid = true;
            cellInfo2.isValid = true;
        }
    }
    function CheckYears(date1, date2) {
        if (!date1 || !date2)
            return null;
        var msecPerYear = 1000 * 60 * 60 * 24 * 365;
        var years = (date2.getTime() - date1.getTime()) / msecPerYear;
        return years;
    }
</script>
<dx:ASPxCheckBox ID="ASPxCheckBox1" runat="server" ClientInstanceName="clientChkb"
    Checked="true" Text="ClientValidation">
</dx:ASPxCheckBox>
<dx:ASPxCheckBox runat="server" ID="ASPxCheckBox2" AutoPostBack="true" OnCheckedChanged="ASPxCheckBox2_CheckedChanged" Checked="false" Text="Validate on the Save Changes button click"></dx:ASPxCheckBox>
<dx:ASPxCardView ClientInstanceName="grid" DataSourceID="AccessDataSource1" KeyFieldName="EmployeeID" ID="ASPxCardView1" OnCardValidating="ASPxCardView1_CardValidating" runat="server" AutoGenerateColumns="False">
    <SettingsEditing Mode="Batch">
    </SettingsEditing>
    <Columns>
        <dx:CardViewTextColumn FieldName="EmployeeID" ReadOnly="True" VisibleIndex="0">
        </dx:CardViewTextColumn>
        <dx:CardViewTextColumn FieldName="LastName" VisibleIndex="1">
        </dx:CardViewTextColumn>
        <dx:CardViewTextColumn FieldName="FirstName" VisibleIndex="2">
        </dx:CardViewTextColumn>
        <dx:CardViewTextColumn FieldName="Address" VisibleIndex="3">
        </dx:CardViewTextColumn>
        <dx:CardViewDateColumn FieldName="BirthDate" VisibleIndex="4" Width="120px">
        </dx:CardViewDateColumn>
        <dx:CardViewDateColumn FieldName="HireDate" VisibleIndex="6" Width="120px">
        </dx:CardViewDateColumn>
    </Columns>
    <CardLayoutProperties>
        <Items>
            <dx:CardViewCommandLayoutItem HorizontalAlign="Right" ShowNewButton="True">
            </dx:CardViewCommandLayoutItem>
            <dx:CardViewColumnLayoutItem ColumnName="EmployeeID">
            </dx:CardViewColumnLayoutItem>
            <dx:CardViewColumnLayoutItem ColumnName="LastName">
            </dx:CardViewColumnLayoutItem>
            <dx:CardViewColumnLayoutItem ColumnName="FirstName">
            </dx:CardViewColumnLayoutItem>
            <dx:CardViewColumnLayoutItem ColumnName="Address">
            </dx:CardViewColumnLayoutItem>
            <dx:CardViewColumnLayoutItem ColumnName="BirthDate">
            </dx:CardViewColumnLayoutItem>
            <dx:CardViewColumnLayoutItem ColumnName="HireDate">
            </dx:CardViewColumnLayoutItem>
            <dx:EditModeCommandLayoutItem HorizontalAlign="Right">
            </dx:EditModeCommandLayoutItem>
        </Items>
    </CardLayoutProperties>
    <ClientSideEvents BatchEditCardValidating="OnValidation" />
</dx:ASPxCardView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/nwind.mdb"
    SelectCommand="SELECT [EmployeeID], [LastName], [FirstName], [Address], [BirthDate], [HireDate] FROM [Employees]"></asp:AccessDataSource>