0

I have added the below custom data annotation validation in my code for my text area (to allow only valid email IDs)

 public class ValidateEmails : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (value != null)
        {
            string[] commaLst = value.ToString().Split(',');
            foreach (var item in commaLst)
            {
                try
                {
                    System.Net.Mail.MailAddress email = new System.Net.Mail.MailAddress(item.ToString().Trim());

                }
                catch (Exception)
                {
                    return new ValidationResult(ErrorMessage = "Please enter valid email IDs separated by commas;");
                }
            }
        }
        return ValidationResult.Success;
    }

}

Model:

 public class BuildModel
{
    public Int64 ConfigID { get; set; }

    [Required(ErrorMessage = "Please select a stream!")]
    public string StreamName { get; set; }

    [Required(ErrorMessage = "Please select a build location!")]
    public string BuildLocation { get; set; }

    public string Type { get; set; }

    public bool IsCoverity { get; set; }

    [ValidateEmails(ErrorMessage = "NOT VALID !!!")]
    public string EmailIDsForCoverity { get; set; }
   }

When I run my app and enter an invalid string in the text area, the breakpoint hits inside the validation. But however, the submit action goes on to happen.

Actually, I have a bootstrap modal form, within which I do the validation. On click of submit button, the inbuilt custom validations like 'Required' work well. However, my custom data annotation validation won't work. What wrong am I doing here?

Ved Prakash
  • 1,390
  • 5
  • 22
  • 31
Ponni
  • 293
  • 5
  • 15
  • 1
    Are you checking for `Model.IsValid` in your action? – DavidG Jul 17 '17 at 11:29
  • you can use `RegularExpression` validators for this and regex for this comma separated validation is `(([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})(\]?)(\s*;\s*|\s*$))* ` please check [this answer](https://stackoverflow.com/a/9809636/2534646) for more info – Curiousdev Jul 17 '17 at 11:32
  • Your attribute needs to implement `IClientValidatable` and you need to write the scripts to add the rules to the `$.validator` if you want client side validation. –  Jul 17 '17 at 12:28
  • Any examples or demo? – Ponni Jul 17 '17 at 12:34
  • Just use data type attributes to validate your field , mvc has an inbuilt one – REDEVI_ Jul 17 '17 at 13:12

2 Answers2

0

You should check Model.IsValid value in controller. Model.IsValid return false if any validation fails(including custom validations). So your controller's code look like below.

  [HttpPost]
  public virtual ActionResult Index(BuildModel viewModel)
  {

    if (ModelState.IsValid)
    {
      // Your Custom code...
    }

    return View(viewModel);
  }
CommonPlane
  • 135
  • 14
0

Your code should look similar to this:

[Display(Name = "Email address")]
[Required(ErrorMessage = "The email address is required")]
[EmailAddress(ErrorMessage = "Invalid Email Address")]
public string Email { get; set; }

Source : Email address validation using ASP.NET MVC data type attributes

REDEVI_
  • 678
  • 8
  • 18