2

I was wondering if someone might be able to help me with this. I need to validate a text input from a textbox. I need to make sure it is in a format acceptable by a TimeSpan. The format expect should be hh:mm:ss:fff (i.e hours, minutes, seconds and milliseconds. e.g 15:30:45:040)

RSolberg
  • 26,821
  • 23
  • 116
  • 160
danielcooperxyz
  • 960
  • 1
  • 13
  • 28

6 Answers6

5

If you are talking about validating on the server side then using TimeSpan.TryParse() is going to be the easiest and most foolproof way.

Steve
  • 8,469
  • 1
  • 26
  • 37
4

I'd mask the input for ##:##:##:### to make sure that you're not getting alpha and special characters out of place.

In order to make sure you're dealing with a valid value on the backend, I'd look to TimeSpan.TryParse() to get you there. Look below.

var val = "00:22:11:424";
TimeSpan ts = new TimeSpan();
TimeSpan.TryParse(val, out ts);

Obviously the TryParse will deal with the exceptions, so you could use TimeSpan.Parse(val) and catch the Exceptions yourself if there was one to display to the user. You could also check the value of ts after the TryParse to make sure that the value is >= TimeSpan.MinValue...

try
{
   var val = "00:22:11:422";
   TimeSpan ts = new TimeSpan();
   ts = TimeSpan.Parse(val);
}
catch(Exception ex)
{
   //do something...
}
RSolberg
  • 26,821
  • 23
  • 116
  • 160
  • That's what I've done already. I was wondering how I would go about returning a error message if the try parse fails? – danielcooperxyz Apr 24 '12 at 16:50
  • @D4nC00per: and where is the problem in returning an error message? – Vlad Apr 24 '12 at 16:51
  • @RSolberg: you don't actually need to call `new TimeSpan()`: having `ts` as `out` parameter guarantees that the value won't be uninitialized after the call. – Vlad Apr 24 '12 at 16:52
  • I'm unsure how to check if the tryparse has succeeded or not. – danielcooperxyz Apr 24 '12 at 16:54
  • 2
    @D4nC00per: `TryParse` returns `bool`, which explicitly says if the parsing was successful. – Vlad Apr 24 '12 at 16:54
  • @D4nC00per this is another option... You could also check the value of ts after the TryParse to make sure that the value is >= TimeSpan.MinValue – RSolberg Apr 24 '12 at 16:56
  • 1
    @RSolberg: `catch(Exception)` is evil, you'd better off with `catch (ArgumentNullException) { ... } catch (FormatException) { ... } catch (OverflowException) { ... }`. – Vlad Apr 24 '12 at 16:58
  • 1
    Thanks guys, you've been extremely helpful. I need to learn how to use this site. – danielcooperxyz Apr 24 '12 at 17:00
  • 1
    @vlad - of course! Not trying to code an entire production ready solution here. I try to help people learn how to fish rather than give them the meal... – RSolberg Apr 24 '12 at 17:00
3

You can use TimeSpan's TryParse method.

hkutluay
  • 6,794
  • 2
  • 33
  • 53
0

I would use TimeSpan.TryParse (MSDN link here) if you are validating on the server side.

bool isValidTimeSpan(string str)
{
   TimeSpan interval;
   if (TimeSpan.TryParse(value, out interval)) return true;
   return false;
}

Also, I would use a masked edit that forces the user to enter the text in the format you are looking for, this will not eliminate all errors, but it will defiantly help cut down the issues.

Muad'Dib
  • 28,542
  • 5
  • 55
  • 68
0

There a lot of way to do this. Use String.Split() and check if there is 4 parts formed by 2 characters and TimeSpan.TryParse().

Omar
  • 16,329
  • 10
  • 48
  • 66
0

look this topic: Matching hours/minutes/seconds in regular expressions - a better way?

Community
  • 1
  • 1
MarcoM
  • 92
  • 1
  • 10