Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Validate Dates in text Box

Posted on 2006-04-10
11
Medium Priority
?
388 Views
Last Modified: 2008-01-09
Hi Experts,

I am having two text boxes.

one is getting start date
second is getting end date .

my problem is  before sending the information.

i need to validate the information in both text boxes  like


1 - format of start date   (SDate.txt)
2 - format of end date    (EDate.txt)

3 - check for that  SDate  must be grerate than EDate

reagrds
bilal
0
Comment
Question by:bsheikh
11 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16417632
1) ASP.NET or WinForms?  
2) .NET version?

Bob
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16417694
Hi bsheikh;

WinForms solution.

        Dim StartDate As DateTime
        Dim EndDate As DateTime
        Dim RetVal As Integer

        Try
        StartDate = Convert.ToDateTime(SDate.Text)
        EndDate = Convert.ToDateTime(EDate.Text)
        Catch
            MessageBox.Show("String Date format error")
            Return
        End Try

        RetVal = DateTime.Compare(StartDate, EndDate)
        If RetVal < 0 Then
            MessageBox.Show("StartDate is less then EndDate")
        ElseIf RetVal = 0 Then
            MessageBox.Show("StartDate is Equal to EndDate")
        Else
            MessageBox.Show("StartDate is Greater Then EndDate")
        End If


Fernando
0
 

Author Comment

by:bsheikh
ID: 16418696
fernando

I tried your code mentioned above.

it is not picking error  if i have this condition

if i have sdate.text = 4/3/200
            edate.text =  4/9/2006  
it should identify this error too.
it is dispalying me error mesg that " start date is less than end date"

but also after displaying that it is executing SP and then i got error from vb.net env that

"An unhandled exception of type 'System.Data.SqlTypes.SqlTypeException' occurred in system.data.dll

Additional information: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM."




regards
bilal


0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bsheikh
ID: 16418726
and also need to know if start date is greater than end date it should stop processing the next statements but stay on the same prompt.

what its doing now is
is displaying mesg that start date is greate than end date  but also jumping to next execution steps.

we need to stay as long as date input is wrong .


regards
bilal
0
 

Author Comment

by:bsheikh
ID: 16418770
Ok fernando

i have solved the questions i asked you again in my last posting .
now need to solve only last thing

that if i have wrong  date in Sdate like 04/02/20----->error in year part

if we have this case. it is skipping the date error handler and executing the store procedure command.



regards
bilal
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16418859
Hi bsheikh;

Microsoft takes this date 04/02/20 to be 04/02/2020 and this date 4/3/200 to be 4/3/0200 and of course this date 4/3/06 to be 4/3/2006.

What format are you requiring your users to enter into the TextBox?

For example:
    m/d/yyyy
    mm/dd/yyyy

Fernando
0
 

Author Comment

by:bsheikh
ID: 16419100
mm/dd/yyyy
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 400 total points
ID: 16419367
Hi bsheikh;

This will check the format of the string as well.

        If Not System.Text.RegularExpressions.Regex.IsMatch( _
            SDate.Text, "^\d\d/\d\d/\d{4}") Then

            ' The user will not be allowed to change focus from this text box
            ' unless the date is in the correct format - mm/dd/yyyy
            MessageBox.Show("Start Date Format Error" & ControlChars.CrLf & _
                "Must be in mm/dd/yyyy format.")
            SDate.Focus()
            Return

        End If

Fernando
        If Not System.Text.RegularExpressions.Regex.IsMatch( _
            EDate.Text, "^\d\d/\d\d/\d{4}") Then

            ' The user will not be allowed to change focus from this text box
            ' unless the date is in the correct format - dd/mm/yyyy
            MessageBox.Show("End Date Format Error" & ControlChars.CrLf & _
                "Must be in mm/dd/yyyy format.")
            EDate.Focus()
            Return

        End If

        Dim StartDate As DateTime
        Dim EndDate As DateTime
        Dim RetVal As Integer

        Try
            StartDate = Convert.ToDateTime(SDate.Text)
            EndDate = Convert.ToDateTime(EDate.Text)
        Catch
            MessageBox.Show("String Date format error")
            Return
        End Try

        RetVal = DateTime.Compare(StartDate, EndDate)
        If RetVal < 0 Then
            MessageBox.Show("StartDate is less then EndDate")
        ElseIf RetVal = 0 Then
            MessageBox.Show("StartDate is Equal to EndDate")
        Else
            MessageBox.Show("StartDate is Greater Then EndDate")
        End If
0
 

Author Comment

by:bsheikh
ID: 16420018
i increased the points .

your above solution is working fine for me .

just curious is it possible that we can provide option that
user may enetr either
m/d/yy   OR


mm/dd/yyyy  

mm/dd/yy


regards
bilal

i appriciate for ur prompt reply
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16420182
Hi bsheikh;

Yes, change this pattern in the two If Statements.

    "^\d\d/\d\d/\d{4}"

To This :

    "^\d{1,2}/\d{1,2}/\d{4}"

The new pattern will accept the following

m/d/yyyy or m/dd/yyyy or mm/d/yyyy or mm/dd/yyyy so the month and day can have 1 or 2 digits each but the year must be 4 digits otherwise someone can enter 02/03/20 which is the year 2020 and something you wanted stoped.

Fernando
0
 
LVL 2

Expert Comment

by:manojkumarps
ID: 16423887
Hello bilal,

       The best way to handle this kind of problem is to create a custom based date textbox where in you can have all the validation and key press events inside the control. Create a textbox class which inherits a textbox and give the format of the date requred by you. Use the textbox created by you in your application as this reduces the number of exceptiuons or errors that you may face with the several validations you are doing before the calculations. Assign properties in the textbox that can provide you woth the difference os the date or whatever is your requirements.

Regards,
Manoj
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

578 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