Solved

Date Validation

Posted on 2010-08-16
17
496 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Linq Help 1 35
Name space syntax error 12 44
Replace &lt; with < 14 57
Filename to be appended with DateTimeof Download 3 47
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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

24 Experts available now in Live!

Get 1:1 Help Now