Validating the date input
Validating date input is an important step to ensure that the input is in the correct format and falls within an acceptable range. Here are the steps to validate date input in an ASP.NET application:
Add a RegularExpressionValidator control to the web form where the date input is located. Set the ControlToValidate property to the ID of the input control.
Set the ValidationExpression property of the RegularExpressionValidator control to a regular expression that matches the expected date format. For example, if the expected date format is “yyyy-MM-dd”, the regular expression should be ^\d{4}-\d{2}-\d{2}$.
Set the ErrorMessage property of the RegularExpressionValidator control to a message that explains the required date format to the user.
Add a RangeValidator control to the web form to validate that the input falls within an acceptable range. Set the ControlToValidate property to the ID of the input control.
Set the Type property of the RangeValidator control to Date.
Set the MinimumValue and MaximumValue properties of the RangeValidator control to the acceptable range of dates.
Set the ErrorMessage property of the RangeValidator control to a message that explains the acceptable range of dates to the user. With these steps, the date input will be validated to ensure that it is in the correct format and falls within an acceptable range. If the input is invalid, the user will be shown an error message and will be prompted to correct the input.
One way to prevent the error is to make sure the date is recognizable and valid before sending it to the database. In this section, you use a CustomValidator control and (gasp!) a line of server-side code to keep bad dates from getting near SQL Server. To validate the date, follow these steps
- Click the FormView control’s Smart Tag to open the Tasks menu.
- Choose Edit Templates.
- From the drop-down list, choose the InsertItemTemplate.
- From the Validation category of the Toolbox, drag a CustomValidator control and drop it below Date Added.
- Set the following CustomValidator properties (F4):
Property | Value |
ControlToValidate | DateAddedTexBox |
Display | Dynamic |
Error Message | Not a valid date! |
SetFocusOnError | True |
ValidateEmptyText | True |
- Switch to Source view and put the cursor between the <script runat=”server”> and </script> tags.
- In the Object drop-down list (top area of the source code frame), choose CustomValidator1.
- From the Event drop-down list (to the right of the Object list), choose ServerValidate. The IDE inserts a procedure that handles the CustomValidator1 ServerValidate event. Insert the following line of code above the closing End Sub
args.IsValid = IsDate(args.Value)
The code uses Visual Basic’s IsDate() function to determine whether the contents of the text box can be converted into a usable date.
Browse to the page and insert a bogus date. The CustomValidator is watching during the ServerValidate event.
Apply for ASP.NET Certification Now!!
https://www.vskills.in/certification/certified-aspnet-programmer