0

I'm writing custom validator attribute in asp.net mvc, Its working fine for server side validation. Here's demo code

 public class CustomEmailValidator : ValidationAttribute,IClientValidatable
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (value != null)
        {
            string email = value.ToString();

            if (Regex.IsMatch(email, @"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}", RegexOptions.IgnoreCase))
            {
                return ValidationResult.Success;
            }
            else
            {
                return new ValidationResult(ErrorMessage);//"Please Enter a Valid Email.");
            }
        }
        else
        {
            return new ValidationResult("" + validationContext.DisplayName + " is required");
        }
    }
    //new method
    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var rule = new ModelClientValidationRule();
        rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName());
        rule.ValidationType = "emailvalidate";

        yield return rule;
    }
}

Now for client side validation, I'm unable to add adapter to obstrusive.js.

$.validator.unobtrusive.adapters.add("emailvalidate", function (options) {

options.messages['emailvalidate'] = options.message;
    });

$.validator.addMethod("emailvalidate", function (value, element) {
     {
         console.log("inside emailValidate function");
         if (value=="test") {
             return true;
         }
         else {
             return false;
         }
    }

});

I'm not fluent in adding adapters so I'm sure that there's issue in adding adapter to obstrusive.js. kindly point out the issue. Thanks.

MUT
  • 576
  • 3
  • 18
  • 2
    In the `$.validator.unobtrusive.adapters.add` method, you need to add the rules to the validator. But all you seem to be validating is a regex, in which case, you should be just creating a ValidationAttribute that extends `RegularExpressionAttribute` (and registering it) so that the scripts are all handled automatically –  Mar 25 '17 at 06:58
  • Well validating a regex is just a demo. I need to add rules to the validator. Would you share some helpful material for that? – MUT Mar 25 '17 at 07:05
  • That depends - what is the real code and what are you actually validating? –  Mar 25 '17 at 07:06
  • For example, I need to add rules for validating a regex (not extending RegularExpressionAttribute ). – MUT Mar 25 '17 at 07:08
  • What do you think `RegularExpressionAttribute` does - its adds the rules for validating against a regex :) If that is your real code, then do not do it - just extend the `RegularExpressionAttribute` (the whole thing, including client side validation is done in a couple of lines of code) –  Mar 25 '17 at 07:11
  • I know, I can extend 'RegularExpressionAttribute'. I just need to know how it works. Its just sample code and I just want to get my hands dirty on that code. :) – MUT Mar 25 '17 at 07:13
  • 1
    Then I recommend your read [The Complete Guide To Validation In ASP.NET MVC 3 - Part 2](http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2) –  Mar 25 '17 at 07:16
  • Also a couple of examples [here](http://stackoverflow.com/questions/40199870/how-to-validate-file-type-of-httppostedfilebase-attribute-in-asp-net-mvc-4/40200034#40200034) and [here](http://stackoverflow.com/questions/42032739/asp-net-mvc-custom-multiple-fields-validation/42036235#42036235) –  Mar 25 '17 at 07:19
  • Adding rules was the trick. I just add 'options.rules["emailvalidate"]=true' to my adapter and KABOOOM!! – MUT Mar 25 '17 at 07:44

0 Answers0