?
Solved

Date Validation

Posted on 2010-08-16
17
Medium Priority
?
502 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

801 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