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)
Share
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.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…