Solved

Military time

Posted on 2007-03-30
5
1,226 Views
Last Modified: 2007-12-19
I need a textbox that except military time only. Should I use datetime.now.tostring(format...? Thank you in advance.
For example right now is 9:10 pm - military time is  2110
0
Comment
Question by:VBdotnet2005
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:Sinoj Sebastian
ID: 18828086
Try this

<asp:TextBox id="TBTime" style="Z-INDEX: 101; LEFT: 128px; POSITION: absolute; TOP: 152px" runat="server"></asp:TextBox>
                  <asp:RegularExpressionValidator id="RegularExpressionValidator1" style="Z-INDEX: 102; LEFT: 296px; POSITION: absolute; TOP: 152px"
                        runat="server" ErrorMessage="Invalid Time" ValidationExpression="(([01][0-9])|(2[0-3]))[0-5][0-9]" ControlToValidate="TBTime"></asp:RegularExpressionValidator>
                  <asp:Button id="BtnSubmit" style="Z-INDEX: 103; LEFT: 208px; POSITION: absolute; TOP: 192px"
                        runat="server" Text="Test" Width="72px"></asp:Button>
                  <asp:Label id="Label1" style="Z-INDEX: 104; LEFT: 56px; POSITION: absolute; TOP: 152px" runat="server">Enter time</asp:Label>

Tell me if you have any problem.
0
 

Author Comment

by:VBdotnet2005
ID: 18828579
If a user input 0651, for example, how can you tell whether it is am or pm, for those whom is not awared of it? Since it excepts it anyway.
0
 
LVL 12

Expert Comment

by:Sinoj Sebastian
ID: 18828685
I'm sorry, i don't understand what you are taking about.

0000 - 1159 is AM
1200 - 2359 is PM

try this
    Private Sub BtnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSubmit.Click
        Dim Time As Date
        Dim a As Char() = Trim(TBTime.Text).ToCharArray()
        TBTime.Text() = Date.Parse(a(0) + a(1) + ":" + a(2) + a(3)).ToShortTimeString
    End Sub
0
 

Author Comment

by:VBdotnet2005
ID: 18828751
Can you help me further? What is a user accidently input just a number? It is going to create an error, correct?
0
 
LVL 12

Accepted Solution

by:
Sinoj Sebastian earned 500 total points
ID: 18828914
I've used RegularExpressionValidator in the above code. If client is java script enabled, then if validation failed the page will not post back. You can use "if page.isvalid then" to confirm that user input is correct.

    Private Sub BtnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSubmit.Click
        If Page.IsValid Then
            Dim a As Char() = Trim(TBTime.Text).ToCharArray()
            TBTime.Text() = Date.Parse(a(0) + a(1) + ":" + a(2) + a(3)).ToShortTimeString
        End If
    End Sub

see the following link for further details
http://msdn2.microsoft.com/en-us/library/eahwtc9e.aspx

One problem with RegularExpressionValidator is that it will accept null string also. So, you may need  RequiredFieldValidator also.

Sorry
I'm late late. Tomorrow is sunday. I can give more detailed information day after tomorrow.
If you need further help, just drop a comment.

PS: I'm at GMT+05:30. Its 10PM now.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question