SlideShare a Scribd company logo
Presented By:
Jyotasana Bharti
 Validation Scenario
 What is Data Annotation
 Data Annotation Attributes
 How to work with Data Annotation Attributes
 Example
 Summary
 References
What to
Validate
• Property
• Entity
• Domain
Operation
When to
Validate
• During data
input
• On
submitting
data
• After
submission
of data
Where to
Validate
• Servers only
• Both Server
and client
How do
we write
logic
• Attributes
• Add
required
code
• Custom
Validation
Class
 key aspect for developing web application.
 used to configure your model classes, which will
highlight the most commonly needed
configurations.
 provides instant user input checking facility at
client side and provides to asp developer, a wide
range of validator attributes. Data Annotation
makes the validating process easy and quick.
 can be used after adding following namespace:
System.ComponentModel.DataAnnotations using
System.ComponentModel;
System.ComponentModel.DataAnnotations in
cludes the following attributes that impacts
the nullability or size of the column.
 Required
 Key
 Timestamp
 ConcurrencyCheck
 MinLength
 MaxLength
 StringLength
 System.ComponentModel.DataAnnotations.
Schema namespace includes the following
attributes that impacts the schema of the
database.
 Table
 Column
 Index
 ForeignKey
 NotMapped
 InverseProperty
 Required
Specifies that Input field cannot be empty.
Example:
1. [Required(ErrorMessage = "Name is Required")]
2. public string Name { get; set; }
 DisplayName
Specifies the display name for a property.
Example:
1. [DisplayName("Enter Your Name: ")]
2. public string Name { get; set; }
 StringLength
Specifies minimum and maximum length for a property.
Example:
1. [StringLength(50, MinimumLength = 3)]
2. public string Name { get; set; }
 Range
Specifies a range of numeric value.
Example:
1. [Range(1,120, ErrorMessage ="Age must be between 1-120 in years.")]
2. public int Age { get; set; }
 Bind
Include or Exclude value when adding form values to model properties.
Example:
[Bind(Exclude = "Id")]
 MaxLength
Specifies maximum length of string.
Example:
1. [MaxLength(50)]
2. public string Name { get; set; }
 Phone
Specifies that an input field value is well-formed phone number using Regular
Expression.
Example:
1. [DataType(DataType.PhoneNumber)]
2. [RegularExpression(@"^(?([0-9]{2})[-. ]?([0-9]{4})[-. ]?(
[0-9]{3})[-. ]?([0-9]{3})$", ErrorMessage = "Not a valid P
hone number")]
3. public string Name { get; set; }
 For creating model, StudentModel.cs
Then create a form in index.cshtml page.
Next add HomeController.cs with the following code:
Once we have define validation to the model by using data
annotations, these are automatically used by Html Helpers in views.
For client side validation to work, please ensure that below two
<SCRIPT> tag references are in the view.
Validate Model
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"
type="text/javascript"></script> <script
src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"
type="text/javascript"></script>
When no data is entered then it will look like as:
After entering wrong data it looks like as
follows:
How to use Data Annotation for validating
input field in ASP.NET MVC 5 has been
explained with example.
Different Data Annotation Attributes are
explored.
 https://www.tutorialspoint.com/asp.net_mv
c/asp.net_mvc_data_annotations.htm
 https://www.slideshare.net/EyalV/aspnet-
mvc-30-validation
 https://www.dotnettricks.com/learn/mvc/m
vc-data-annotations-for-model-validation
Data annotation validation (ASP.net)

More Related Content

What's hot

Test Automation
Test AutomationTest Automation
Test Automation
rockoder
 
Test automation principles, terminologies and implementations
Test automation principles, terminologies and implementationsTest automation principles, terminologies and implementations
Test automation principles, terminologies and implementations
Steven Li
 
Setting up Page Object Model in Automation Framework
Setting up Page Object Model in Automation FrameworkSetting up Page Object Model in Automation Framework
Setting up Page Object Model in Automation Framework
valuebound
 
ReactJS presentation.pptx
ReactJS presentation.pptxReactJS presentation.pptx
ReactJS presentation.pptx
DivyanshGupta922023
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
ikhwanhayat
 
Integration test
Integration testIntegration test
Integration test
sadegh salehi
 
C#
C#C#
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured frameworkTesting RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
Micha Kops
 
Test automation process
Test automation processTest automation process
Test automation process
Bharathi Krishnamurthi
 
Entity framework code first
Entity framework code firstEntity framework code first
Entity framework code firstConfiz
 
Validation controls in asp
Validation controls in aspValidation controls in asp
Validation controls in asp
Shishir Jain
 
Test Automation Best Practices (with SOA test approach)
Test Automation Best Practices (with SOA test approach)Test Automation Best Practices (with SOA test approach)
Test Automation Best Practices (with SOA test approach)
Leonard Fingerman
 
html-css
html-csshtml-css
Introduction to the Web API
Introduction to the Web APIIntroduction to the Web API
Introduction to the Web API
Brad Genereaux
 
Asp.NET Validation controls
Asp.NET Validation controlsAsp.NET Validation controls
Asp.NET Validation controls
Guddu gupta
 

What's hot (20)

Test Automation
Test AutomationTest Automation
Test Automation
 
Test automation principles, terminologies and implementations
Test automation principles, terminologies and implementationsTest automation principles, terminologies and implementations
Test automation principles, terminologies and implementations
 
Hybrid framework
Hybrid frameworkHybrid framework
Hybrid framework
 
Setting up Page Object Model in Automation Framework
Setting up Page Object Model in Automation FrameworkSetting up Page Object Model in Automation Framework
Setting up Page Object Model in Automation Framework
 
ReactJS presentation.pptx
ReactJS presentation.pptxReactJS presentation.pptx
ReactJS presentation.pptx
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
 
Js ppt
Js pptJs ppt
Js ppt
 
C# Delegates
C# DelegatesC# Delegates
C# Delegates
 
Integration test
Integration testIntegration test
Integration test
 
C#
C#C#
C#
 
Testing RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured frameworkTesting RESTful Webservices using the REST-assured framework
Testing RESTful Webservices using the REST-assured framework
 
Test automation process
Test automation processTest automation process
Test automation process
 
Sdi & mdi
Sdi & mdiSdi & mdi
Sdi & mdi
 
Javascript validating form
Javascript validating formJavascript validating form
Javascript validating form
 
Entity framework code first
Entity framework code firstEntity framework code first
Entity framework code first
 
Validation controls in asp
Validation controls in aspValidation controls in asp
Validation controls in asp
 
Test Automation Best Practices (with SOA test approach)
Test Automation Best Practices (with SOA test approach)Test Automation Best Practices (with SOA test approach)
Test Automation Best Practices (with SOA test approach)
 
html-css
html-csshtml-css
html-css
 
Introduction to the Web API
Introduction to the Web APIIntroduction to the Web API
Introduction to the Web API
 
Asp.NET Validation controls
Asp.NET Validation controlsAsp.NET Validation controls
Asp.NET Validation controls
 

Similar to Data annotation validation (ASP.net)

HTML Forms
HTML FormsHTML Forms
HTML Forms
Nisa Soomro
 
Dat402
Dat402Dat402
Dat402
ssa2010
 
Advanced dot net
Advanced dot netAdvanced dot net
Advanced dot net
ssa2010
 
HTML-Forms
HTML-FormsHTML-Forms
HTML-Forms
Ahmed Saihood
 
MuleSoft London Community February 2020 - MuleSoft and OData
MuleSoft London Community February 2020 - MuleSoft and ODataMuleSoft London Community February 2020 - MuleSoft and OData
MuleSoft London Community February 2020 - MuleSoft and OData
Pace Integration
 
Asp.NET MVC
Asp.NET MVCAsp.NET MVC
Asp.NET MVC
vrluckyin
 
Embedded Typesafe Domain Specific Languages for Java
Embedded Typesafe Domain Specific Languages for JavaEmbedded Typesafe Domain Specific Languages for Java
Embedded Typesafe Domain Specific Languages for JavaJevgeni Kabanov
 
Visual studio 2008
Visual studio 2008Visual studio 2008
Visual studio 2008Luis Enrique
 
Defending against Injections
Defending against InjectionsDefending against Injections
Defending against Injections
Blueinfy Solutions
 
Apex Testing and Best Practices
Apex Testing and Best PracticesApex Testing and Best Practices
Apex Testing and Best Practices
Jitendra Zaa
 
introduction to Windows Comunication Foundation
introduction to Windows Comunication Foundationintroduction to Windows Comunication Foundation
introduction to Windows Comunication Foundation
redaxe12
 
HTML - FORMS.pptx
HTML - FORMS.pptxHTML - FORMS.pptx
HTML - FORMS.pptx
Nyssakotian
 
Asp.net mvc training
Asp.net mvc trainingAsp.net mvc training
Asp.net mvc training
icubesystem
 
Linq intro
Linq introLinq intro
Linq intro
Bình Trọng Án
 
Secure Dot Net Programming
Secure Dot Net ProgrammingSecure Dot Net Programming
Secure Dot Net ProgrammingAdam Getchell
 
Unit 1part-2 forms & frames
Unit 1part-2 forms & framesUnit 1part-2 forms & frames
Unit 1part-2 forms & frames
Govardhan Bhavani
 
Day2_Apache_JMeter_Script_Enhancements
Day2_Apache_JMeter_Script_EnhancementsDay2_Apache_JMeter_Script_Enhancements
Day2_Apache_JMeter_Script_Enhancements
Sravanthi N
 
SFDC Inbound Integrations
SFDC Inbound IntegrationsSFDC Inbound Integrations
SFDC Inbound Integrations
Sujit Kumar
 

Similar to Data annotation validation (ASP.net) (20)

HTML Forms
HTML FormsHTML Forms
HTML Forms
 
Dat402
Dat402Dat402
Dat402
 
Advanced dot net
Advanced dot netAdvanced dot net
Advanced dot net
 
HTML-Forms
HTML-FormsHTML-Forms
HTML-Forms
 
MuleSoft London Community February 2020 - MuleSoft and OData
MuleSoft London Community February 2020 - MuleSoft and ODataMuleSoft London Community February 2020 - MuleSoft and OData
MuleSoft London Community February 2020 - MuleSoft and OData
 
Asp.NET MVC
Asp.NET MVCAsp.NET MVC
Asp.NET MVC
 
Embedded Typesafe Domain Specific Languages for Java
Embedded Typesafe Domain Specific Languages for JavaEmbedded Typesafe Domain Specific Languages for Java
Embedded Typesafe Domain Specific Languages for Java
 
Visual studio 2008
Visual studio 2008Visual studio 2008
Visual studio 2008
 
Defending against Injections
Defending against InjectionsDefending against Injections
Defending against Injections
 
Apex Testing and Best Practices
Apex Testing and Best PracticesApex Testing and Best Practices
Apex Testing and Best Practices
 
introduction to Windows Comunication Foundation
introduction to Windows Comunication Foundationintroduction to Windows Comunication Foundation
introduction to Windows Comunication Foundation
 
HTML - FORMS.pptx
HTML - FORMS.pptxHTML - FORMS.pptx
HTML - FORMS.pptx
 
Asp.net mvc training
Asp.net mvc trainingAsp.net mvc training
Asp.net mvc training
 
Linq intro
Linq introLinq intro
Linq intro
 
Secure Dot Net Programming
Secure Dot Net ProgrammingSecure Dot Net Programming
Secure Dot Net Programming
 
B_110500002
B_110500002B_110500002
B_110500002
 
Chapter09
Chapter09Chapter09
Chapter09
 
Unit 1part-2 forms & frames
Unit 1part-2 forms & framesUnit 1part-2 forms & frames
Unit 1part-2 forms & frames
 
Day2_Apache_JMeter_Script_Enhancements
Day2_Apache_JMeter_Script_EnhancementsDay2_Apache_JMeter_Script_Enhancements
Day2_Apache_JMeter_Script_Enhancements
 
SFDC Inbound Integrations
SFDC Inbound IntegrationsSFDC Inbound Integrations
SFDC Inbound Integrations
 

Recently uploaded

MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
Kamal Acharya
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
Kamal Acharya
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
Kamal Acharya
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
MuhammadTufail242431
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
PrashantGoswami42
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 

Recently uploaded (20)

MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 

Data annotation validation (ASP.net)

  • 2.  Validation Scenario  What is Data Annotation  Data Annotation Attributes  How to work with Data Annotation Attributes  Example  Summary  References
  • 3. What to Validate • Property • Entity • Domain Operation When to Validate • During data input • On submitting data • After submission of data Where to Validate • Servers only • Both Server and client How do we write logic • Attributes • Add required code • Custom Validation Class
  • 4.  key aspect for developing web application.  used to configure your model classes, which will highlight the most commonly needed configurations.  provides instant user input checking facility at client side and provides to asp developer, a wide range of validator attributes. Data Annotation makes the validating process easy and quick.  can be used after adding following namespace: System.ComponentModel.DataAnnotations using System.ComponentModel;
  • 5. System.ComponentModel.DataAnnotations in cludes the following attributes that impacts the nullability or size of the column.  Required  Key  Timestamp  ConcurrencyCheck  MinLength  MaxLength  StringLength
  • 6.  System.ComponentModel.DataAnnotations. Schema namespace includes the following attributes that impacts the schema of the database.  Table  Column  Index  ForeignKey  NotMapped  InverseProperty
  • 7.  Required Specifies that Input field cannot be empty. Example: 1. [Required(ErrorMessage = "Name is Required")] 2. public string Name { get; set; }  DisplayName Specifies the display name for a property. Example: 1. [DisplayName("Enter Your Name: ")] 2. public string Name { get; set; }  StringLength Specifies minimum and maximum length for a property. Example: 1. [StringLength(50, MinimumLength = 3)] 2. public string Name { get; set; }  Range Specifies a range of numeric value. Example: 1. [Range(1,120, ErrorMessage ="Age must be between 1-120 in years.")] 2. public int Age { get; set; }
  • 8.  Bind Include or Exclude value when adding form values to model properties. Example: [Bind(Exclude = "Id")]  MaxLength Specifies maximum length of string. Example: 1. [MaxLength(50)] 2. public string Name { get; set; }  Phone Specifies that an input field value is well-formed phone number using Regular Expression. Example: 1. [DataType(DataType.PhoneNumber)] 2. [RegularExpression(@"^(?([0-9]{2})[-. ]?([0-9]{4})[-. ]?( [0-9]{3})[-. ]?([0-9]{3})$", ErrorMessage = "Not a valid P hone number")] 3. public string Name { get; set; }
  • 9.  For creating model, StudentModel.cs
  • 10. Then create a form in index.cshtml page. Next add HomeController.cs with the following code: Once we have define validation to the model by using data annotations, these are automatically used by Html Helpers in views. For client side validation to work, please ensure that below two <SCRIPT> tag references are in the view. Validate Model
  • 12. After entering wrong data it looks like as follows:
  • 13. How to use Data Annotation for validating input field in ASP.NET MVC 5 has been explained with example. Different Data Annotation Attributes are explored.