Solved

Date Validation

Posted on 2010-08-16
17
497 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Close form "before" open 3 40
Windows Media Player 2 26
Finally almost got it 5 36
C# bracket error 3 31
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

773 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