Solved

Military time

Posted on 2007-03-30
5
1,231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

690 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