Jump to content
  • Advertisement
Sign in to follow this  
chrisliando

[.net] Time Validation Problem

This topic is 3935 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I am having a problem with validating user input as a Time type. I have 2 textboxes, each represents Hour and Minute. if user click Ok button then I will check if their time input is valid or not. If I use my code below, I will be able to handle input like 00,01,... but this cannot handle invalid time outside my range which I set for minute to be 00-59, hour 00-23. How to solve it? //checking time validity DateTime ^supphour = gcnew DateTime(1,1,1,Convert::ToInt32(txtSuppHour->Text->Trim()),Convert::ToInt32(txtSuppMin->Text->Trim()),0); DateTime ^finhour = gcnew DateTime(1,1,1,Convert::ToInt32(txtFinHour->Text->Trim()),Convert::ToInt32(txtFinMin->Text->Trim()),0); if (!(supphour->Hour >= 0 && supphour->Hour <= 23) || !(supphour->Minute >= 0 && supphour->Minute <= 59) || !(finhour->Hour >= 0 && finhour->Hour <= 23) || !(finhour->Minute >= 0 && finhour->Minute <= 59)) { MessageBox:how("Invalid time value." + "\n" + "Please check either support time value or finish time value and re-enter.", "Customer Support System", MessageBoxButtons:K, MessageBoxIcon::Warning ); e->Cancel = true; txtSuppHour->Focus(); invalid = true; } Thank you very much.

Share this post


Link to post
Share on other sites
Advertisement
Why are you doing with DateTime?

Try like that:
int hour = Int32.Parse(txtHour.Text); // or TryParse
int min = Int32.Parse(txtMinutes.Text);
if (hour < 0 || hour > 23 || min < 0 || min > 59)
//error

You could also do that with DateTimePicker, setting Format on Time and ShowUpDown on True.

Share this post


Link to post
Share on other sites
Quote:
Original post by chrisliando
//checking time validity
DateTime ^supphour = gcnew DateTime(1,1,1,Convert::ToInt32(txtSuppHour->Text->Trim()),Convert::ToInt32(txtSuppMin->Text->Trim()),0);
DateTime ^finhour = gcnew DateTime(1,1,1,Convert::ToInt32(txtFinHour->Text->Trim()),Convert::ToInt32(txtFinMin->Text->Trim()),0);
if (!(supphour->Hour >= 0 && supphour->Hour <= 23) || !(supphour->Minute >= 0 && supphour->Minute <= 59) || !(finhour->Hour >= 0 && finhour->Hour <= 23) || !(finhour->Minute >= 0 && finhour->Minute <= 59)) {
MessageBox:how("Invalid time value." + "\n" + "Please check either support time value or finish time value and re-enter.", "Customer Support System", MessageBoxButtons:K, MessageBoxIcon::Warning );
e->Cancel = true;
txtSuppHour->Focus();
invalid = true;
}

Thank you very much.


If you choose to use the DateTime objects as shown here (and not simple integer checks as suggested above) you're going to have to prevalidate the data before attempting to construct a DateTime variable with it. The checks you perform are also done by the DateTime constructor - if your data is invalid, yes, the DateTime constructor is going to throw an exception. No way around that.

Pseudocode:

.. Verify minutes are valid.
.. Verify hours are valid.
.. Return invalid if necessary.
.. Construct support date.
.. Construct finish date.
.. Return valid.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!