TextBox inside GridView validation

This is my code:

<asp:TemplateField HeaderText="Email">
    <ItemTemplate> 
        <asp:TextBox ID="txtEmail" runat="server" Text='<%# Eval("Email") %>' Width="88px" CausesValidation="True" />
        <asp:RegularExpressionValidator runat="server" ErrorMessage="Enter a valid email id!" ValidationExpression="w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*" ControlToValidate="txtEmail" />
        <asp:RequiredFieldValidator runat="server" ErrorMessage="*" ControlToValidate="txtEmail" />
    </ItemTemplate>
</asp:TemplateField>

when i am clicking next button required field validation is not causing validation and its not showing any error and page is re-directing on next page.

Please help me.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Check out the following code.

enter image description here

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="grd" runat="server" AutoGenerateColumns="false">
            <Columns>
            <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
                <asp:TemplateField HeaderText="Email">

                    <ItemTemplate>
                        <asp:TextBox ID="txtEmail" runat="server" Text='<%# Eval("Email") %>' Width="88px"
                            CausesValidation="True" />
                        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Enter a valid email id!"
                            ValidationExpression="w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*" ControlToValidate="txtEmail" />
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
                            ControlToValidate="txtEmail" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    <asp:Button ID="Button1" runat="server" Text="Button" CausesValidation="true" />
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var items = new[] {
                new { FirstName = "Name1",Email="<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="542735143339353d387a373b39">[email protected]</a>" },
                new { FirstName = "Name2",Email="<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f88c9d8b8cb89f95999194d69b9795">[email protected]</a>" },
                new { FirstName = "Name3",Email="<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="136776606753747e727a7f3d707c7e">[email protected]</a>" }};

        grd.DataSource = items;

        grd.DataBind();
    }
}

Method 2

There might be the problem with the Validation group, you can check that.

Secondly you need to set the CausesValidation="true" in your button control.

Plus your email regular expression is not correct as well.

w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)* // yours
w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)* // correct one

Method 3

Did you add Script Manager to your project ?

If so, nothing wrong with your validation code.

Just an idea, maybe you should add to validation controls EnableClientScript="false"

Method 4

You would need to check if CausesValidation is enabled for the next button you are clicking.

Also in addition try setting the InitialValue-“” for the Required field validator

EDIT

Try modifying your code to adding the client script such as

button.Attributes.Add("onclick","return methodname");

Method 5

<script type="text/javascript" language="javascript">
function ValidateText(i) 
{
    if(i.value.length>0) 
    {
    i.value = i.value.replace(/[^d]+/g, ''); 
    }
}

</script>

<asp:TemplateField >
      <ItemTemplate>
              <asp:TextBox ID="TextBox1" runat="server" onkeyup ="ValidateText(this);"></asp:TextBox>
     </ItemTemplate>

</asp:TemplateField>  

<    /asp:TemplateField>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x