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?

5
  • 1
    Are you checking for Model.IsValid in your action?
    – DavidG
    Jul 17, 2017 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 for more info
    – Curiousdev
    Jul 17, 2017 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.
    – user3559349
    Jul 17, 2017 at 12:28
  • Any examples or demo?
    – Ponni
    Jul 17, 2017 at 12:34
  • Just use data type attributes to validate your field , mvc has an inbuilt one
    – REDEVI_
    Jul 17, 2017 at 13:12

2 Answers 2

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);
  }
2
  • But I need it to be validated at client side only.
    – Ponni
    Jul 17, 2017 at 12:10
  • 2
    Then you should use jquery validation. Custom validation work on server side. Jul 17, 2017 at 12:12
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

1
  • My input would be comma separated e-mail IDs, not a single email ID.
    – Ponni
    Jul 17, 2017 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.