We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to achieve the required field validation for DatePicker control in ASP.NET MVC?

Client and Server side validations for EJ Form controls:

The list of form controls available in the Essential Studio are as follows:

  1. DatePicker.
  2. DateTimePicker.
  3. Numeric, Currency, and Percentage Textbox.
  4. Autocomplete and Dropdown list.
  5. Mask Edit.
  6. Checkbox and Radio button.

You can add both client-side and server-side validations for these components. The following section explains how to achieve this.

 

To add client side validation:

JQuery validation:

An inbuilt jQuery validation support is available for the form controls. jQuery validation can be achieved via ValidationRules and ValidationMessage properties. The ValidationRules property is used to define the jQuery validate rules for the control and ValidationMessage is used to define the respective error message for the rules that are specified in the ValidationRules property. When error message is not specified to the validation rules, it takes the default validation error message. The ValidationRules and ValidationMessage properties work within a form element, but otherwise it does not work. Before using those properties, you need to add the jQuery validate plugin (jquery.validate.min.js) to your application. This is showcased in the following code example.

 

 

HTML5 Validation:

You can get the HTML 5 validation in the component with help of the HtmlAttributes API. This API is used to add the HTML attributes to the respective elements. The HtmlAttributes is not only for HTML 5 validation, but it can also add the list of possible HTML attributes to the element. This is demonstrated in the following code example.

 

Server Side Validation:

You can set the validation for the EJ Form control in the form post back by using the server-side validation. Follow the steps to achieve this.

  1. In model, apply Data Annotation attributes on the properties. In controller, validate the received data within the action method by checking the ModelState.IsValid property. In case it is false, then redirect to the view page with the error message for invalid values.

ModelState.isValid returns false when the input data is not valid, otherwise it returns true.

The following code examples displays the same.

 

 

 

  1. When you press the submit button on this page, then it posts the data to the server and the code written within Index action validates the input data by checking the ModelState.IsValid property. When the input data is not valid, then the ModelState.IsValid returns false and shows error.

 

 

 

 

               

 

 

Article ID: Published Date: Last Revised Date: Platform: Control:
5433 09/21/2015 09/21/2015 ASP.NET MVC DatePicker
Did you find this information helpful?
Add Comment
You must log in to leave a comment

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon