Validate Dates in text Box

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
bsheikhAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
1) ASP.NET or WinForms?  
2) .NET version?

Bob
0
Fernando SotoRetiredCommented:
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
bsheikhAuthor Commented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

bsheikhAuthor Commented:
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
bsheikhAuthor Commented:
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
Fernando SotoRetiredCommented:
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
bsheikhAuthor Commented:
mm/dd/yyyy
0
Fernando SotoRetiredCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bsheikhAuthor Commented:
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
Fernando SotoRetiredCommented:
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
manojkumarpsCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.