How do I do these pls
1- to check if a user enters the correct time for ex: 10:15 PM
2- if a user enters for example 3:5 i want to reformat that and display 03:05 instead
3- to check if a user enters a date correctly: 2007-12-26
How do I do these pls
1- to check if a user enters the correct time for ex: 10:15 PM
2- if a user enters for example 3:5 i want to reformat that and display 03:05 instead
3- to check if a user enters a date correctly: 2007-12-26
how do u check if an entered time or date is valid
TruePsion
Hi,
you can use the DateTime.TryParse http://msdn2.microsoft.com/en-us/library/9h21f14e.aspx you can pass a string to this method as well as a formatting string to tell the method how it should interpret the data. If the string value is in the correct format then it will return a valid DateTime object in the out parameter.
Mark.
regthesk8r
Hi. One practical way to solve this kind of problems is by using Regular Expressions (Regex class in System.Text.RegularExpressions)
private void btnCheckInput_Click(object sender, EventArgs e)
{
Regex reDate = new Regex(@"^(\d{4})(-\d{2}-\d{2})$");
Regex reTime = new Regex(@"^(\d{1,2})((:\d{1,2}) (PM))$");
if (reDate.IsMatch(txtDate.Text))
{
MessageBox.Show("Date OK");
}
else
{
MessageBox.Show("Date Error");
return;
}
if (reTime.IsMatch(txtTime.Text))
{
MessageBox.Show("Time OK");
}
else
{
MessageBox.Show("Time Error");
return;
}
}
Check this link: http://msdn2.microsoft.com/en-us/library/az24scfc(VS.80).aspx
Bye!