0

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.

4
Contributors
12
Replies
16
Views
7 Years
Discussion Span
Last Post by devi.....
Featured Replies
  • [QUOTE=Anupama G;1013756]ASP.net[/QUOTE] Well Anupama, this is C# forum, you should post this thread on ASP.net forum..... well on button click event try to implement this........... [CODE] if(textbox1.text=" ") { MessageBox.Show("Please enter the right value"); } [/CODE] Hope ir help you............... Mark this thread solved if it help you.................... Read More

  • [QUOTE=Anupama G;1013765]Sorry for posting on the wrong forum. Thanks for your reply[/QUOTE] Oh no problem........:) Well tell me, this code works for you or not........ Read More

  • Hi Anupama, Try the following code. .aspx code [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> [/CODE] C# Code behind [CODE] 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 … Read More

  • [QUOTE=Anupama G;1013771]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.[/QUOTE] See [URL="http://www.daniweb.com/forums/thread137454.html"]this link[/URL] And [URL="http://www.aspsnippets.com/post/2009/02/08/TextBox-Validation-using-JavaScript.aspx"]this link[/URL] hope it help you.................. Read More

0

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............?

4

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....................

0

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

2

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

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

0

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.

3

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.

Edited by Ramesh S: n/a

1

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..................

0

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

0

Hi Ramesh,

Thanks a lot.It works

Well Anupama mark this thread as solved, if your problem is solved

0
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;
        }
    }}

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?

Edited by mike_2000_17: Fixed formatting

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.