1

So I've got entities set up with Entity Framework that have some properties of types String, DateTimeOffset and TimeSpan. For performing validation on the Strings, I've used fairly straightforward attributes like "[Required]" or "[StringLength]", and the control used is a textbox.

My issue now is for the DateTimeOffset and TimeSpan, I'm uncertain what control to use, but even more uncertain about what attributes to use to validate the input.

Any suggestions?

1
  • Input in a web browser typically comes in as text, and MVC can coerce it into a type, but if I have "OOGABOOGA", it certainly won't work as a DateTimeOffset. Commented Nov 27, 2010 at 21:14

2 Answers 2

0

I don't believe there is a way to infer the data type from input. Treating it as needed by business logic seems to be the only path.

Sign up to request clarification or add additional context in comments.

Comments

-1

Since it's a timespan, you can get the minutes equivalent and do an IsNumeric call on that - which is a simple custom validation function.

If your calculating the time diff using two dates, you can use the DateDiff function to calculate the difference and get the timespan - but if you already have this, remember a timespan can be represented as a number of units of time, so use IsNumeric using a custom function.

Otherwise, if you don't want to write a custom validation, the. The easiest way to do it is use the RangeValidator Attribute. Place in the min and max possible numbers, just to be safe, and when validation occurs using this attribute, it will return False if its not a numeric number anyway, so you get bad string validation from that alone, and you are also able to validate the range of possible values of the timespan - if range isn't important, then just set the upper limit to a safe value like several million minutes or something (eg: Long.MaxValue or Integer.MaxValue).

You can also pass Ticks as the value of the timespan to compare to the range, so TimeSpan.Ticks() I believe, but just sending the text box to RangeValidator is enough, it will check that it ain't text characters and check for range and voila, validation done and no need for custom validation.

Let me know how you get along and how else I can help you if needed.

3 Comments

@byte did you get a chance to look at the possible solution?
Your answer certainly makes sense, but I guess I wasn't really after a way to validate the data types themselves once you obtain them. The purpose behind the question was, given a choice, what input type would you use (textbox, for example); And given the input, how would you validate that it is indeed a DateTimeOffset? Your answer assumes you already have the data and that you know its of the right type.
@byte If you don't know what you will be receiving and want to figure it out during validation, you can either send two hidden fields as dates and you'll know for sure, or one text box field as ticks (timespan ticks) and since this number will always be a very large long, you can always check to see if it's a numeric and above a certain million or not. If this doesn't answer your question, then I'm afraid I probably haven't understood the question entirely. If it's still a problem, please elaborate with more detail by perhaps updating ur question. Cool?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.