Grid Custom Validation

3 posts, 0 answers
  1. Virgil Rodriguez
    Virgil Rodriguez avatar
    70 posts
    Member since:
    Oct 2008

    Posted 17 Oct Link to this post

    Hi Telerik,

     

    1. Is there a way to validate a GridBoundColumn so that it does the following:

    • if it is empty, display a "This field is required!" message
    • allow only numbers and up to 3 digits only

    2. Is there a way to validate another GridBoundColumn so that it does the following:

    • if it is empty, display a "This field is required!" message
    • allow only one-character letters, specifically ("A","B","C"), if input is not any of these 3, to display an error message.

    3. How do I convert No. 2 above to a GridDropdownColumn and also satisfy the same validation requirements? Dropdown option should display:

    • Please pick a fruit
    • A=Apple
    • B=Banana
    • C=Corn

    Thanks as always.

  2. Virgil Rodriguez
    Virgil Rodriguez avatar
    70 posts
    Member since:
    Oct 2008

    Posted 18 Oct in reply to Virgil Rodriguez Link to this post

    I was able to figure out what I need. Here is what I came up with:

     

    <telerik:GridTemplateColumn HeaderText="Hrs. Offered" DataField="MIN_OFFERED" UniqueName="MIN_OFFERED" ShowFilterIcon ="false" AutoPostBackOnFilter="false" HeaderStyle-Width="45px" >
        <ItemTemplate>
            <asp:Label  ID="lblMIN_OFFERED" runat="server" Width="30px" Text='<%#DataBinder.Eval(Container.DataItem, "MIN_OFFERED") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtMIN_OFFERED" runat="server" Width="45px" MaxLength="2" TextMode="Number" ReadOnly="false"></asp:TextBox>
            <asp:RegularExpressionValidator
                    runat="server"
                    ControlToValidate="txtMIN_OFFERED"
                    ErrorMessage="0 to 24 only"
                    Display="Dynamic"
                    ForeColor="Red"
                    Font-Size="X-Small"
                    ValidationExpression="^([01]?\d|24)$">
            </asp:RegularExpressionValidator>
            <asp:RequiredFieldValidator ControlToValidate="txtMIN_OFFERED" ErrorMessage="Hrs. Offered is required" runat="server"
                ForeColor="Red" Font-Size="X-Small" Display="Dynamic"></asp:RequiredFieldValidator>
        </EditItemTemplate>
    </telerik:GridTemplateColumn>
    <telerik:GridTemplateColumn HeaderText="Response" DataField="RESPONSE" datatype="System.Char"  UniqueName="RESPONSE" ShowFilterIcon ="false" AutoPostBackOnFilter="false" HeaderStyle-Width="45px" >
        <ItemTemplate>
            <asp:Label ID="lblRESPONSE" runat="server" Width="45px" Text='<%#DataBinder.Eval(Container.DataItem, "RESPONSE") %>'>></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtRESPONSE" runat="server" Width="30px" MaxLength="1" ReadOnly="false"></asp:TextBox>
            <asp:RequiredFieldValidator ControlToValidate="txtRESPONSE" ErrorMessage="Response is required" runat="server"
                ForeColor="Red" Display="Dynamic" Font-Size="X-Small"></asp:RequiredFieldValidator>
            <asp:RegularExpressionValidator
                    runat="server"
                    ControlToValidate="txtRESPONSE"
                    ErrorMessage="Invalid entry! Only A, R, N, M, T, C and X please."
                    Display="Dynamic"
                    ForeColor="Red"
                    Font-Size="X-Small"
                    ValidationExpression="^[ARNMTCX]{1}$"
                    >
            </asp:RegularExpressionValidator>
        </EditItemTemplate>
    </telerik:GridTemplateColumn>

     

    But I still need your help on No. 3 please.

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1798 posts

    Posted 20 Oct Link to this post

    Hi Virgil,

    GridBoundColumn and GridDropDownColumn exposes only a RequiredFieldValidator option and for custom validators you need to use GridTemplateColumn:
    <telerik:GridDropDownColumn ..>
        <ColumnValidationSettings ..>
            <RequiredFieldValidator ..></RequiredFieldValidator>
        </ColumnValidationSettings>
    </telerik:GridDropDownColumn>

    If you need to allow the user to select an existing value on you can use the above approach and take a look at the following for help topic for populating and customizing the DropDown editor:
    Hope this helps.


    Regards,
    Konstantin Dikov
    Telerik by Progress
    Check out the new UI for ASP.NET Core, the most complete UI suite for ASP.NET Core development on the market, with 60+ tried-and-tested widgets, based on Kendo UI.
Back to Top