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

Server side validation not working in RadGrid

3 Answers 227 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Swetha
Top achievements
Rank 1
Swetha asked on 26 Nov 2012, 11:32 PM

Hi,
I am using RegularExpressionValidator for validating email id for a particular company. ValidationExpression=\\w+([-+.']\\w+)*@ABCCompany.com working for only first email id, i need to validate more than one email. I found in online ValidationExpression="((\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*([;])*)*", its working fine but i have to validate for ABCCompany.com. I tried also CustomValidator server side validation, its working code wise but its not showing error message.
Can you please anybody solve my problem.


My HTML Code:
-------------
<

telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" CellPadding="0"  CellSpacing="0" GridLines="None" HorizontalAlign="Left" ShowFooter="true" AllowPaging="true"  PageSize="10" AllowMultiRowSelection="true" OnItemCommand="RadGrid1_OnItemCommand" OnNeedDataSource="RadGrid1_OnNeedDataSource" OnInsertCommand="RadGrid1_OnInsertCommand" OnUpdateCommand="RadGrid1_OnUpdateCommand" OnDeleteCommand="RadGrid1_OnDeleteCommand"  AllowSorting="true">

 <PagerStyle Position="Bottom" AlwaysVisible="true" />

 <ValidationSettings EnableValidation="true" ValidationGroup="products" />

<MasterTableView CommandItemDisplay="Top" HorizontalAlign="NotSet" AutoGenerateColumns="False" DataKeyNames="id">

 <Columns>
<telerik:GridTemplateColumn DataField="Product_ID" FilterControlAltText="Filter Product_ID column" HeaderText="Product_ID" UniqueName="Product_ID" SortExpression="Product_ID">

<HeaderStyle HorizontalAlign="Center" />

<ItemStyle HorizontalAlign="Center" />

<ItemTemplate>

<asp:Label ID="lblProduct_ID" runat="server" Text='<%# Bind("Product_ID") %>'></asp:Label>

</ItemTemplate>

</telerik:GridTemplateColumn>

<telerik:GridTemplateColumn DataField="Product_Email" FilterControlAltText="Filter Product_Email column"

HeaderText="Product Email" UniqueName="Product_Email" SortExpression="Product_Email">

<HeaderStyle HorizontalAlign="Left" />

<ItemStyle HorizontalAlign="Left" />

<ItemTemplate>

<asp:Label ID="lblProduct_Email" runat="server" Text='<%# Bind("Product_Email") %>'></asp:Label>

</ItemTemplate>

<EditItemTemplate>

<asp:TextBox ID="tbProduct_Email" runat="server" Text='<%# Bind("Product_Email") %>'

Width="500px"></asp:TextBox>&nbsp;(Note: Enter multiple emails with ; separator)

<asp:RequiredFieldValidator ID="rfvtbProduct_Email" runat="server" ControlToValidate="tbProduct_Email"

ErrorMessage="Please enter Product Email" ForeColor="Red" ValidationGroup="products"></asp:RequiredFieldValidator>

<%

--<asp:RegularExpressionValidator ID="revtbProduct_Email" runat="server" ControlToValidate="tbProduct_Email"

 ErrorMessage="Enter only valid ABCCompany Emails" ForeColor="Red" ValidationExpression="\\w+([-+.']\\w+)*@ABCCompany.com"

ValidationGroup="products"></asp:RegularExpressionValidator>--%>

<asp:CustomValidator ID="cvtbProduct_Email" runat="server" ControlToValidate="tbProduct_Email"

ForeColor="Red" ErrorMessage="Enter only valid ABCCompany Emails" OnServerValidate="cvtbProduct_Email_OnServerValidate"

ValidationGroup="products" EnableClientScript="false" Display="None"></asp:CustomValidator>

</EditItemTemplate>

</telerik:GridTemplateColumn>

<telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">

<ItemStyle HorizontalAlign="Center"></ItemStyle>

</telerik:GridEditCommandColumn>

<telerik:GridButtonColumn ButtonType="ImageButton" CommandName="Delete" Text="Delete"

UniqueName="DeleteColumn">

<ItemStyle HorizontalAlign="Center"></ItemStyle>

</telerik:GridButtonColumn>

</Columns>

<EditFormSettings ColumnNumber="1" CaptionDataField="Product_id" CaptionFormatString="Edit properties of Product ID: {0}"  InsertCaption="Product ID:">

</EditFormSettings>

</MasterTableView>

<ClientSettings EnableRowHoverStyle="true">

</ClientSettings>

</telerik:RadGrid>


Severside validation Code:
--------------------------

protected
void cvtbProduct_Email_OnServerValidate(object sender, ServerValidateEventArgs args)

 {

 

  bool valid;

 string[] email = new string[100];

TextBox EmailList = (TextBox)RadGrid1.MasterTableView.GetInsertItem().FindControl("Product_Email");

if (EmailList.Text != "")

{

if (EmailList.Text.Contains(";"))

 email = EmailList.Text.Split(';');

else

email[0] = EmailList.Text;

for (int i = 0; i < email.Length; i++)

{

if (email[i] != null)

{

valid = Regex.IsMatch(email[i], "\\w+([-+.']\\w+)*@ABCCompany.com");

if (!valid)

{

args.IsValid = false;

 }

else

args.IsValid = true;

 

}

}

}

}

 

 Thanks,
Swetha.

3 Answers, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 29 Nov 2012, 01:54 PM
Hi Swetha,

There are two possible ways you can validate the email while editing:
  1. Client-side using validators and a textbox like in the following code snippet:
    <telerik:GridTemplateColumn DataField="Email" UniqueName="TemplateColumn1">
                        <ItemTemplate>
                            <%# Eval("Email")%>
                            </ItemTemplate>
                        <EditItemTemplate>
                            <telerik:RadTextBox ID="Radtextbox2" runat="server"  TextMode="MultiLine" Text='<%# Bind("Email") %>' />
                            <asp:RegularExpressionValidator ID="emailValidator" runat="server" Display="Dynamic"
                            ValidationGroup="Group1" CssClass="Validator" ErrorMessage="Please, enter valid e-mail address."
                            ValidationExpression="^[\w\.\-]+@[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]{1,})*(\.[a-zA-Z]{2,3}){1,2}$"
                            ControlToValidate="Radtextbox2">
                                </asp:RegularExpressionValidator>
                            <asp:RequiredFieldValidator ID="Requiredfieldvalidator1" runat="server" Display="Dynamic"
                            ValidationGroup="Group1" CssClass="Validator" ControlToValidate="Radtextbox2"
                            ErrorMessage="Please, enter an e-mail!"></asp:RequiredFieldValidator>
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>
  2. By intercepting the OnUpdateCommand as I see you have and there if validation has not passed cancel the command like this:
    protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
                //check if the email is valid;
                e.Canceled = true;
        }
    Additionally in this demo you can see how to display a user-friendly message that will alert that validation has not passed.
All the best,
Angel Petrov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Swetha
Top achievements
Rank 1
answered on 29 Nov 2012, 03:09 PM
Hi Telerik Team,

Thanks for your reply. Its working fine but my requrement was different. I want to apply for a common company. The RegularExpressionvalidator should be validate only one particular provider email address. For example telerik@gmail.com , controls@gmail.com , grid@gmail.com like this. Here @gmail.com was common to all emails. I need all emails validate only one represent company.
0
Angel Petrov
Telerik team
answered on 04 Dec 2012, 11:52 AM
Hello Swetha,

The problem in this case is not related to RadControls. It is a developers responsibility to implement its custom logic. You just have to replace the expression in the example I provided with a custom one that suits your case.

All the best,
Angel Petrov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Swetha
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Swetha
Top achievements
Rank 1
Share this question
or