Solved

Date Validation

Posted on 2010-08-16
17
495 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
  • 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now