[RESOLVED] validating date and time

Hi,

I have 2 textboxes in my page.  One is supposed to hold the Date and the other Time.  These 2 data are merged when saving into one database field having a datetime datatype.  Upon retrieve, the datetime field value is split into 2 and put into the Date Textbox and Time TextBox.

Upon input, when I am trying to update, the validator always says "Invalid Time."

Please help!

Thanks for you input!

Tinac99

 

Designer Page

===========

Date: <asp:TextBox ID="TextBox1" runat="server" CssClass="DDTextBox" Text='<%# (FieldValueEditString != "") ? String.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(FieldValueEditString)) : "" %>' Columns="20"></asp:TextBox>

Time<asp:TextBox ID="TextBox2" runat="server" CssClass="DDTextBox" Text='<%# (FieldValueEditString != "") ? String.Format("{0:hh:mm tt}",Convert.ToDateTime(FieldValueEditString)) : "" %>'></asp:TextBox>

<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" CssClass="DDControl DDValidator" ControlToValidate="TextBox1" Display="Static" Enabled="false" ErrorMessage="Please Enter Date." />


<asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator1"
    CssClass="DDControl DDValidator" ControlToValidate="TextBox1" Display="Static"
    Enabled="false" ErrorMessage="Please enter Date."
    ValidationExpression="(([0]?[1-9]|1[012])[- /.]([0]?[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d)|((1[012]|0[1-9])(3[01]|2\d|1\d|0[1-9])(19|20)\d\d) " />


<asp:DynamicValidator runat="server" ID="DynamicValidator1" CssClass="DDControl DDValidator" ControlToValidate="TextBox1" Display="Static" />


<asp:CustomValidator runat="server" ID="DateValidator" CssClass="DDControl DDValidator" ControlToValidate="TextBox1" Display="Static" EnableClientScript="false" Enabled="false" OnServerValidate="DateValidator_ServerValidate" />

<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator2" CssClass="DDControl DDValidator" ControlToValidate="TextBox2" Display="Static" Enabled="false"/>


<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
    ControlToValidate="TextBox2"  ErrorMessage="Please enter Time."
    ValidationExpression="{0:t}"></asp:RegularExpressionValidator>

<asp:CustomValidator runat="server" ID="DateValidator2" CssClass="DDControl DDValidator" ControlToValidate="TextBox2" Display="Static" EnableClientScript="false" Enabled="false" OnServerValidate="DateValidator_ServerValidate" />

 

 

I think the time validation expression is not quite right...check out...

And also provide more details on what user input it is breaking....?? and also if possible some code...on how you are formatting the date and time together....

Hi,

tinac99

<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
    ControlToValidate="TextBox2"  ErrorMessage="Please enter Time."
    ValidationExpression="{0:t}"></asp:RegularExpressionValidator>
 

 

"{0:t}" is not a regular expression but a date time format specifier.

DateTime dateTime = System.DateTime.Now;
   Response.Write(string.Format("{0:t}", dateTime));


The output will be the format time such as "2:43 PM","4:50 AM".

If you want to use regular expression to validate above time, you can try this:

^ *(1[0-2]|[1-9]):[0-5][0-9] *(a|p|A|P)(m|M) *$

In validator control:

<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
    ControlToValidate="TextBox2"  ErrorMessage="Please enter Time."
    ValidationExpression="^ *(1[0-2]|[1-9]):[0-5][0-9] *(a|p|A|P)(m|M) *$"></asp:RegularExpressionValidator>

Hope this can help you.

hey

try using the range validator?

<asp:RangeValidator runat="server" ID="DateRV" ErrorMessage="Incorrect Date"
                        ControlToValidate="DateTxt" Text="*"></asp:RangeValidator>
     DateRV.MinimumValue = DateTime.MinValue.ToString("dd/MM/yyyy");
            DateRV.MaximumValue = DateTime.MaxValue.ToString("dd/MM/yyyy");


 

I don't think this can be done in asp.net. I've been trying to validate a datetime string using min & max dates and there doesn't seem to be any way to get this to work correctly.

thoughts on "[RESOLVED] validating date and time"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>