Hello guys,
since I have now effectively completed my first asp.net application (it's a very basic application where people...ahem, where I can record overtime) I was wondering what the best way is to add some validation. The page which allows you to upload the details needed (date, hours worked and comments) is very simple but two of the fields (date and hours worked)need to be validated. Being as I have many times repeated at the very beginning of my asp.net/visual studio/C#/SQL trip, I thought I'd take a look at some of the Visual Studio 3.5 inbuilt validations but I soon discovered that they are very limited (oh unless you can download more of course): I added. So, I was hoping to get some advice as to what to do.
Here is a screenshot of the input page, I thought it'd be nice to put things in context:
Basically I need the date and hours fields to be validated against no input and invalid input and I have no idea what to do and what to use. THe date fields should be input in any this format 30/12/2014 or 30/12/2014 or 30.12.2014 or 30/12/14 etc (obviously any advice is accepted) and the hours should be in the range 0-168 (which I could do easily in visual studio with the range validation but then I realized that somehow it had to include the possibility that the field was empty, so I discarded the inbuilt range validation).
Ah, the source code of that page is here, just in case, I don't know
http://pastebin.com/aAfDksUB (Default.aspx)
http://pastebin.com/D8thkAFG (Default.aspx.cs)
Recommended Answers
Jump to PostTHe date fields should be input in any this format
Is there a specific reason you haven't used a DateTimePicker?
For an empty field you can use the RequiredFieldValidator. You can add as many validators to a field as you please. There's also the CustomFieldValidator if you want …
Jump to PostI can add the range validation + the RequiredField and what about the fact that the input can only be a number
You could choose to use a CustomValidator and do all those checks in code. Am not sure if the range validator does a numeric check, but …
Jump to Postso theoretically, I could have both the range validator and the required field one with 2 separate error messages?
Correct.
I also had a look at the calendar
Sorry about that, I have so many tools installed I often forget it's not native to .NET.
Jump to PostWith the asp.net validators, you should have all of your bases covered. Overtime, you'll find the custom validator to be your friend once you learn a bit if regex.
Jump to PostSomething that hasn't been mentioned but worth bringing up regarding asp.net validation controls...not only do they handle client side validation, but they also take care of server side validation in the event that javascript is disabled on the browser.
Validating server side is very important.
All 20 Replies
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.