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.

9
  • 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 Commented Mar 25, 2017 at 6: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? Commented Mar 25, 2017 at 7:05
  • That depends - what is the real code and what are you actually validating? Commented Mar 25, 2017 at 7:06
  • For example, I need to add rules for validating a regex (not extending RegularExpressionAttribute ). Commented Mar 25, 2017 at 7:08
  • 1
    Then I recommend your read The Complete Guide To Validation In ASP.NET MVC 3 - Part 2 Commented Mar 25, 2017 at 7:16

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.