Today I will explain about data anntations for MVC in ASP.NET.
Do you know Data Annotations?
Data Annotations is a library in the .NET Framework that helps in validation. The developer doesn’t need to create the logic for the validation since the validation can be done by specifying the data annotation attributes that forces the data validation rules.
Namespace
The namespace System.ComponentModel.DataAnnotations provides the attribute classes for validations.
Attribute Classes
There are more than 30 attribute classes in the System.ComponentModel.DataAnnotations namespace. We will discuss some of the most commonly used attributes.
- RequiredAttribute
- Range Attribute
- EmailAddressAttribute
- MinLengthAttribute
- MaxLengthAttribute
Required Attribute
1 2 |
[Required(ErrorMessage = "Please enter Name)] public string Name{ get; set; } |
The Error message property is used to get/set the error message.
Range Attribute
The Range attribute forces the range validation for the data field.
Example
1 2 |
[Range(1.00, 100.00,ErrorMessage="Please enter a Amount between 1.00 and 100.00" )] public decimal? Amount { get; set; } |
EmailAddressAttribute
The Email address attribute forces the validation and checks to ensure that the field has a valid email ID.
Example
1 2 |
[EmailAddress(ErrorMessage = "Please enter a valid Email address")] public string email { get; set; } |
Regular Expression Attribute
Regular expressions can be added to a data field. The added regular expression will be forced for validation.
Some of most commonly used snippets for validation
1 |
[RegularExpression(@"^[0-9]{8,11}$", ErrorMessage = "error Message ")] |
Used for checking the field to have only numbers and min of 8 digits and max of 11 digits.
1 |
[RegularExpression(@"[0-9]{0,}\.[0-9]{2}", ErrorMessage = "error Message")] |
Used for checking the field to have only numbers with 2 decimal places.
1 |
[RegularExpression(@"^(?!00000)[0-9]{5,5}$", ErrorMessage = "error Message")] |
Used for validation Zip code. The Zip code should contain 5 digits and should not be 00000.
1 |
[RegularExpression(@"^(\d{4})$", ErrorMessage = "error Message")] |
The field should contain 4 digit numbers. Used for limiting the characters and allowing only numbers.
1 |
[RegularExpression(@"^(\d{5,9})$", ErrorMessage = "error Message")] |
The field should contain a minimum of 5 digit numbers and a maximum of 9 digit numbers.
Regex for validating Email.
1 2 3 |
@"^(([^<>()[\]\\.,;:\s@""]+(\.[^<>()[\]\\.,;:\s@""]+)*)|("".+""))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$" @"^(?!0+$)[0-9]{5,5}$" |
Used while validating the Zip codes. Also the number should not start with “0”.
MinLengthAttribute
Validates the minimum length of the data field (String/Array). It will not work for int types.
MaxLengthAttribute
Validates the maximum length of the data field (String/Array). It will not work for int types.
Enabling Unobtrusive Client-side Validation via data Annotaions
When we include the following changes in the config file, the client-side validation will be done based on the data annotation rules that we have defined in the model.
1 2 |
<add key="ClientValidationEnabled" value="true" /> lt;add key="UnobtrusiveJavaScriptEnabled" value="true" /> |
The following scripts should be added to the view page to enable Unobtrusive Client-side Validation via data Annotations.
1 2 3 |
<script src="~/Scripts/jquery-2.1.1.js"></script> <script src="~/Scripts/jquery.validate.min.js"></script> <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script |
No #1 Recommended ASP.NET MVC Hosting
ASPHostPortal.com
ASPHostPortal.com is the leading provider of Windows hosting and affordable ASP.NET MVC Hosting. ASPHostPortal proudly working to help grow the backbone of the Internet, the millions of individuals, families, micro-businesses, small business, and fledgling online businesses. ASPHostPortal has ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2015, .NET 5/ASP.NET 4.5.2, ASP.NET MVC 6.0/5.2, Silverlight 6 and Visual Studio Lightswitch, ASPHostPortal guarantees the highest quality product, top security, and unshakeable reliability, carefully chose high-quality servers, networking, and infrastructure equipment to ensure the utmost reliability.
HostForLIFE.eu
HostForLIFE.eu guarantees 99.9% uptime for their professional ASP.NET MVC hosting and actually implements the guarantee in practice. HostForLIFE.eu is the service are excellent and the features of the web hosting plan are even greater than many hosting. HostForLIFE.eu offer IT professionals more advanced features and the latest technology. Relibility, Stability and Performance of servers remain and TOP priority. Even basic service plans are equipped with standard service level agreements for 99.99% uptime. Advanced options raise the bar to 99.99%. HostForLIFE.eu revolutionized hosting with Plesk Control Panel, a Web-based interface that provides customers with 24×7 access to their server and site configuration tools.
DiscountService.com.au
DiscountService.com.au is The Best and Cheap ASP.NET MVC Hosting. DiscountService.com.au was established to cater to an under served market in the hosting industry web hosting for customers who want excellent service. DiscountService.com.au guarantees the highest quality product, top security, and unshakeable reliability, carefully chose high-quality servers, networking, and infrastructure equipment to ensure the utmost reliability. DiscountService.com.au has ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2015, .NET 5/ASP.NET 4.5.2, ASP.NET MVC 6.0/5.2, Silverlight 6 and Visual Studio Lightswitch. DiscountService.com.au is devoted to offering the best Windows hosting solution for you.