1

I need your help, I have wrote this class to hold the data, here in my example I used the DataAnnotation for validation, unfortunately I entered in invalid email address but it did not object so I am confused about what is the correct way to use the DataAnnotations

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace workflow.DataHolders
{
    public class NewCompany
    {
        [Required]
        [StringLength(200, MinimumLength = 3, ErrorMessage="Length Should  Be More Than Three Letters")]
        public string CompanyName { get; set; }

        [Required]
        [EmailAddress(ErrorMessage="Invalid Email Address")]
        public string Email { get; set; }

        [Required]
        [StringLength(200, MinimumLength = 2, ErrorMessage = "Length Should  Be More Than Two Letters")]
        public string Country { get; set; }

        public string Description { get; set; }
    }
}
  • What do you mean by valid Email address? can you put an example? – Ala Jun 16 '15 at 13:54
  • 1
    take a look at this answer, it's well explained http://stackoverflow.com/a/26329350/4773983 – Enrique Zavaleta Jun 16 '15 at 13:55
  • @Ala I edited it, invalid* –  Jun 16 '15 at 13:55
  • 1
    What to you mean _"it did not object"_? What are you expecting? Do your have client side validation enabled? Have you included `@Html.ValidationMessageFor()` helpers in the view. Do you check `ModelState.IsValid` in the POST method? –  Jun 16 '15 at 13:57

1 Answers1

0

We have two way to do this

1st Way

[RegularExpression(@"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", ErrorMessage = "Invalid Email Address")]
public string email { get; set; }

2nd Way

 [DataType(DataType.EmailAddress)]
 [Email]
 public string email { get; set; }
hutchonoid
  • 32,982
  • 15
  • 99
  • 104
Power Star
  • 1,724
  • 2
  • 13
  • 25