Click here to Skip to main content
15,881,742 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can anybody explain me the following code??


C#
[Required(ErrorMessage = "The email field is required.")]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.")]
//[DataType(DataType.EmailAddress)]
[RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Please enter a valid email")]
[Display(Name = "Email")]
public string ContactEmail { get; set; }
Posted
Comments
DamithSL 15-Dec-14 1:34am    
why not read the documentation? refer some tutorials documentations first, then ask specific question.
check these...[^]
Member 11242836 15-Dec-14 2:23am    
thnks... it just went above my head n i didnt even knew what to search..

1 solution

These are 4 attribuits applied to the given property, and normally they are used in the context of validation, and here are the meaning:
1. Required - mark the property as required, and if the user forgot to enter a value for this property the associated message will be shown.
2. StringLength - set the max length of the string to 100, but the error message is not set correctly in this case. See details here: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.stringlengthattribute(v=vs.110).aspx[^]
3. RegularExpression set the regular expression that will be used to validate the user input.
4. Display - set the default display name for this property. This will be used in ASP.NET MVC in the generated Views as the name of the label for this property.
 
Share this answer
 
Comments
Member 11242836 15-Dec-14 2:21am    
Thnks... :)
M new to it...
Raul Iloc 15-Dec-14 2:31am    
Welcome, I am glad that I could help you!
Raul Iloc 15-Dec-14 2:33am    
You could see these attributes in work in my next article: http://www.codeproject.com/Articles/526827/MVC-Basic-Site-Step-Multilingual-Site-Skeleton
Member 11242836 17-Dec-14 2:07am    
sure :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900