This is a migrated thread and some comments may be shown as answers.

Grid Custom Validation

2 Answers 198 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Virgil Rodriguez
Top achievements
Rank 1
Virgil Rodriguez asked on 17 Oct 2016, 06:53 PM

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 Answers, 1 is accepted

Sort by
0
Virgil Rodriguez
Top achievements
Rank 1
answered on 18 Oct 2016, 04:53 PM

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.

0
Konstantin Dikov
Telerik team
answered on 20 Oct 2016, 10:52 AM
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.
Tags
Grid
Asked by
Virgil Rodriguez
Top achievements
Rank 1
Answers by
Virgil Rodriguez
Top achievements
Rank 1
Konstantin Dikov
Telerik team
Share this question
or