Solved

Date Validation

Posted on 2010-08-16
17
501 Views
Last Modified: 2012-05-10
I am writing windows application
Hi I need a global validating function to validate date in DD/MM/YYYY format. If user enters in any other format other than DD/MM/YYYY it should not accept the date
0
Comment
Question by:GRChandrashekar
[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
  • 9
  • 4
  • 3
  • +1
17 Comments
 
LVL 9

Expert Comment

by:Shahid Thaika
ID: 33444507
Are you using .Net? If so, you can use its "Validation Controls". It makes things easy for the developer.
0
 
LVL 9

Expert Comment

by:Shahid Thaika
ID: 33444534
On a second thought, something to consider is how anyone or the system can differentiate between dd/mm/yy and mm/dd/yy? Granted it is obvious if the date input is 12/31/10, but what if both the day and month inputs are less than or equal to 12 - 11/04/10? I think it may be better to check for a valid date input.

Declare a function that returns a Boolean. You can return True, if date is valid. If not valid, you can cancel the date change event.

BTW, validation only exists in ASP.Net afaik, but people have implemented their own for Windows applications.

See: http://www.codeproject.com/KB/miscctrl/validatingtextbox.aspx

Nevertheless, you will still be stuck on the above matter regarding dd/mm v/s mm/dd!
0
 

Author Comment

by:GRChandrashekar
ID: 33444632
Yes I am using.Net how  can I use its "Validation Controls".
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:GRChandrashekar
ID: 33444768
Validation controls is a good idea I can use it for all controls. but where can i get it
0
 
LVL 27

Expert Comment

by:nmarun
ID: 33444862
Try overriding the _Validating event of the control.

http://bytes.com/topic/c-sharp/answers/545482-there-global-validate-winform

So it'll be something like:

private void textBox1_Validating(object sender, System.CancelEventArgs e)
 {
     e.Cancel = ValidateDate(testBox1.Text);
 }
 
 private bool ValidateDate(string text)
 {
     string message = "";
     if (... some test ...)
      {
           message = "Date is not valid.";
      }
      errorProvider1.SetError(message);
      return message.Length == 0;
 }

ARun
0
 

Author Comment

by:GRChandrashekar
ID: 33444976
I am basically looking for validator controls to validate controls on form
0
 

Author Comment

by:GRChandrashekar
ID: 33445125
Now the problem is If i write something in validating event like this

 private void textBox1_Validating(object sender, CancelEventArgs e)
        {
            string error = null;
            if (textBox1.Text.Length == 0)
            {
                error = "Please enter a name";
                e.Cancel = true;
            }
            errorProvider1.SetError((Control)sender, error);

        }

IT WILL NOT EVEN ALLOW ME TO CLOSE THE FORM IF CONTROL IS INVALID HOW TO OVERCOME THIS
0
 

Author Comment

by:GRChandrashekar
ID: 33446021
Any Help ?
0
 
LVL 3

Expert Comment

by:yanoch
ID: 33446024
Just user a MaskedTextBox.

Name the MaskedTextBox as  mtxt_TextBox

add this to your constructor :

            mtxt_TextBox.Mask =  "00/00/0000";
            mtxt_TextBox.ValidatingType = typeof(System.DateTime);
            mtxt_TextBox.TypeValidationCompleted += new TypeValidationEventHandler(mtxt_TextBox_TypeValidationCompleted);

add this to your form code.

        void mtxt_TextBox_TypeValidationCompleted(object sender, TypeValidationEventArgs e)
        {
            //this will empty the maskedtextbox if the format is invalid.
            mtxt_TextBox.Text = "";
        }
0
 

Author Comment

by:GRChandrashekar
ID: 33446091
My question was
IT WILL NOT EVEN ALLOW ME TO CLOSE THE FORM IF CONTROL IS INVALID HOW TO OVERCOME THIS

private void textBox1_Validating(object sender, CancelEventArgs e)
        {
            string error = null;
            if (textBox1.Text.Length == 0)
            {
                error = "Please enter a name";
                e.Cancel = true;
            }
            errorProvider1.SetError((Control)sender, error);

        }

 
0
 

Author Comment

by:GRChandrashekar
ID: 33446097
OR how to use .net Validation Controls
0
 
LVL 3

Expert Comment

by:yanoch
ID: 33446126
sorry i did a mistake,

use this instead.

Just user a MaskedTextBox.

Name the MaskedTextBox as  mtxt_TextBox

add this to your constructor :

            mtxt_TextBox.Mask =  "00/00/0000";
            mtxt_TextBox.ValidatingType = typeof(System.DateTime);
            mtxt_TextBox.TypeValidationCompleted += new TypeValidationEventHandler(mtxt_TextBox_TypeValidationCompleted);

add this to your form code.

        void mtxt_TextBox_TypeValidationCompleted(object sender, TypeValidationEventArgs e)
        {
            //this will empty the maskedtextbox if the format is invalid.
             if (!e.IsValidInput)
                mtxt_TextBox.Text = "";        }
0
 
LVL 3

Expert Comment

by:yanoch
ID: 33446150
The e.Cancel = true prevent you from closing the form. If you set it like that you can't. so to be able to close the form remove the e.Cancel=true;
0
 

Author Comment

by:GRChandrashekar
ID: 33446205
Hmmm but how do i validate then without setting e.Cancel=true;
not only date i have text box and other controls. is there alternate ?
0
 
LVL 3

Expert Comment

by:yanoch
ID: 33446348

I'm always dooing my final validation before saving the form.

You can set the error provider but let the user move to another field (leave the control) by removing the e.Cancel=true;

just before saving you validate everithing.

and if something is wrong you set the error provider for every error in your form.
0
 
LVL 9

Accepted Solution

by:
Shahid Thaika earned 500 total points
ID: 33447773
Remind me whether the unload even fires before or after the validating event. If before try this alternative in addition to the above. Declare a boolean variable and set the default value to be false. Set the value to be true in the Form_Unload event. Check for this value in the validating event. If it is set to true, then ask the user if he wants to close the window or not. If yes, then instead of cancel=true, do a form unload.

If unload event fires after the validating event, try to do the above using some other event, else on possible solution would be to show the user a message box asking whether he wants to clear the value. If Yes is chosen, then instead of cancel=true, set the text property to EmptyString. This way he will be able to close the form and in case the date is a required field, then he will not be able to save the current record.
0
 

Author Closing Comment

by:GRChandrashekar
ID: 33451563
It is tedious process. There are free and third party validation frameworks which is easy to adpot
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

691 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