Hi,

I have a textbox(placed inside a ItemTemplate) inside a gridview.I want to validate the text entered in the textbox for date(mmddyyyy format) values,without using any validation controls.
Could anyone help me out with the C# code.

Thanks in advance.

Recommended Answers

All 12 Replies

Hi,

I have a textbox(placed inside a ItemTemplate) inside a gridview.I want to validate the text entered in the textbox for date(mmddyyyy format) values,without using any validation controls.
Could anyone help me out with the C# code.

Thanks in advance.

Hello Anupama G...........
tell me one thing that this is you want to used in ASP.net or in window Application............?

Hello Anupama G...........
tell me one thing that this is you want to used in ASP.net or in window Application............?

ASP.net

ASP.net

Well Anupama, this is C# forum, you should post this thread on ASP.net forum.....
well on button click event try to implement this...........

if(textbox1.text=" ")
{
MessageBox.Show("Please enter the right value");
}

Hope ir help you...............
Mark this thread solved if it help you....................

Well Anupama, this is C# forum, you should post this thread on ASP.net forum.....
well on button click event try to implement this...........

if(textbox1.text=" ")
{
MessageBox.Show("Please enter the right value");
}

Hope ir help you...............
Mark this thread solved if it help you....................

Sorry for posting on the wrong forum.
Thanks for your reply

Sorry for posting on the wrong forum.
Thanks for your reply

Oh no problem........:)
Well tell me, this code works for you or not........

Oh no problem........:)
Well tell me, this code works for you or not........

I have a problem in accessing the textbox directly as it is placed inside a gridview.I need to make the validation OnTextChanged event of the textBox.

Hi Anupama,

Try the following code.

.aspx code

<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="CustomValidator"></asp:CustomValidator>
        <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"
            OnRowCommand="GridView1_RowCommand">
            <Columns>

                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

C# Code behind

public static bool IsValidDate(string date)
    {
        try
        {

            Regex reDate = new Regex(@"\b([0-9]{1,2}[\s]?[\-/\.\–][\s]?[0-9]{1,2}[\s]?[\-/\.\–][\s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);
            DateTime tempDate = Convert.ToDateTime(date, ifpformat);
            if ((tempDate.Year > 1900) && (tempDate.Year < 2100))
                return true;
            else
                return false;

        }
        catch (System.FormatException)
        {
            return false;
        }
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        string strDate = (sender as TextBox).Text;
        if (!String.IsNullOrEmpty(strDate) && !IsValidDate(strDate))
        {
            CustomValidator1.ErrorMessage = "Invalid date";
            CustomValidator1.IsValid = false;
            return;
        }
    }

In future, post your ASP.NET related questions here.

I have a problem in accessing the textbox directly as it is placed inside a gridview.I need to make the validation OnTextChanged event of the textBox.

See this link And this link
hope it help you..................

Hi Anupama,

Try the following code.

.aspx code

<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="CustomValidator"></asp:CustomValidator>
        <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"
            OnRowCommand="GridView1_RowCommand">
            <Columns>

                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

C# Code behind

public static bool IsValidDate(string date)
    {
        try
        {

            Regex reDate = new Regex(@"\b([0-9]{1,2}[\s]?[\-/\.\–][\s]?[0-9]{1,2}[\s]?[\-/\.\–][\s]?[0-9]{2,4})\b|\b(([0-9]{1,2}[TtHhSsRrDdNn]{0,2})[\s]?[\-/\.,\–]?[\s]?([Jj][Aa][Nn][Uu]?[Aa]?[Rr]?[Yy]?|[Ff][Ee][Bb][Rr]?[Uu]?[Aa]?[Rr]?[Yy]?|[Mm][Aa][Rr][Cc]?[Hh]?|[Aa][Pp][Rr][Ii]?[Ll]?|[Mm][Aa][Yy]|[Jj][Uu][Nn][Ee]?|[Jj][Uu][Ll][Yy]?|[Aa][Uu][Gg][Uu]?[Ss]?[Tt]?|[Ss][Ee][Pp][Tt]?[Ee]?[Mm]?[Bb]?[Ee]?[Rr]?|[Oo][Cc][Tt][Oo]?[Bb]?[Ee]?[Rr]?|[Nn][Oo][Vv][Ee]?[Mm]?[Bb]?[Ee]?[Rr]?|[Dd][Ee][Cc][Ee]?[Mm]?[Bb]?[Ee]?[Rr]?)[\s]?[\-/\.,\–]?[\s]?[']?([0-9]{2,4}))\b|\b(([Jj][Aa][Nn][Uu]?[Aa]?[Rr]?[Yy]?|[Ff][Ee][Bb][Rr]?[Uu]?[Aa]?[Rr]?[Yy]?|[Mm][Aa][Rr][Cc]?[Hh]?|[Aa][Pp][Rr][Ii]?[Ll]?|[Mm][Aa][Yy]|[Jj][Uu][Nn][Ee]?|[Jj][Uu][Ll][Yy]?|[Aa][Uu][Gg][Uu]?[Ss]?[Tt]?|[Ss][Ee][Pp][Tt]?[Ee]?[Mm]?[Bb]?[Ee]?[Rr]?|[Oo][Cc][Tt][Oo]?[Bb]?[Ee]?[Rr]?|[Nn][Oo][Vv][Ee]?[Mm]?[Bb]?[Ee]?[Rr]?|[Dd][Ee][Cc][Ee]?[Mm]?[Bb]?[Ee]?[Rr]?)[\s]?[,]?[\s]?[0-9]{1,2}[TtHhSsRrDdNn]{0,2}[\s]?[,]?[\s]?[']?[0-9]{2,4})\b");
            Match mDate = reDate.Match(date);
            if (!mDate.Success)
                return false;


            System.IFormatProvider ifpformat = new System.Globalization.CultureInfo("en-GB", true);
            DateTime tempDate = Convert.ToDateTime(date, ifpformat);
            if ((tempDate.Year > 1900) && (tempDate.Year < 2100))
                return true;
            else
                return false;

        }
        catch (System.FormatException)
        {
            return false;
        }
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        string strDate = (sender as TextBox).Text;
        if (!String.IsNullOrEmpty(strDate) && !IsValidDate(strDate))
        {
            CustomValidator1.ErrorMessage = "Invalid date";
            CustomValidator1.IsValid = false;
            return;
        }
    }

In future, post your ASP.NET related questions here.

Hi Ramesh,

Thanks a lot.It works

Hi Ramesh,

Thanks a lot.It works

Well Anupama mark this thread as solved, if your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\s]?[,]?[\s]?[']?[0-9]{2,4})\b");
            Match mDate = reDate.Match(date);
            if (!mDate.Success)
                return false;


            System.IFormatProvider ifpformat = new System.Globalization.CultureInfo("en-GB", true);
            DateTime tempDate = Convert.ToDateTime(date, ifpformat);
            if ((tempDate.Year > 1900) && (tempDate.Year < 2100))
                return true;
            else
                return false;

        }
        catch (System.FormatException)
        {
            return false;
        }
        protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        string strDate = (sender as TextBox).Text;
        if (!String.IsNullOrEmpty(strDate) && !IsValidDate(strDate))
        {
            CustomValidator1.ErrorMessage = "Invalid date";
            CustomValidator1.IsValid = false;
            return;
        }
    }}

I am devi .. I want to use validation of a date which was entered in a textbox using Asp.net Ajax Calender Extender . I want to validate it, it's date of birth so i want it like " when some one enters like dateofbirth before a 110 years and dateofbirth greater than todays date are invalid" . I want to try the above code...? If it doesn't work, how i do this validation using a custom valaidator?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.