0

if i want to inject client side validation code with my own custom attribute class then i have to work with IClientValidatable.

here is sample example of IClientValidatable

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(
        ModelMetadata metadata, ControllerContext context)
    {
        var rule = new ModelClientValidationRule();
        rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName());
        rule.ValidationParameters.Add("wordcount", WordCount);
        rule.ValidationType = "maxwords";
        yield return rule;
    }

but right now i do not know what to write in server side code which generate jquery val code at client side. so please give me some suggestion.

thanks

Mou
  • 15,673
  • 43
  • 156
  • 275
  • Not clear what your asking. Your server code does not generate the jquery code (all it does is add `data-val-*` attributes that are read by `jquery.validate.unobutrusive.js` to add rules to `jquery-validate.js`). Refer [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) for how to implement client side validation –  Oct 21 '16 at 11:07
  • what to write code in server function called `GetClientValidationRules` which will inject pwd is strong or not code at client side? – Mou Oct 21 '16 at 11:12
  • Why not just use the built in `[RegularExpression]` attribute? (you have not even said what your criteria for a 'strong' password is - but refer [this answer](http://stackoverflow.com/questions/5142103/regex-to-validate-password-strength) for an example of a regex) –  Oct 21 '16 at 11:15
  • my requirement is that i will have a custom attribute which i can reuse and which will check pwd is strong or not at client side and server side. a) pwd length must be more than 5 b) must have one special char c) must have one digit. – Mou Oct 21 '16 at 11:19
  • Then just use a `[RegularExpression]` attribute. Why do you want to reinvent the wheel? –  Oct 21 '16 at 11:20
  • can i use it `[RegularExpression(@"^(?=.*[A-Z].*[A-Z])(?=.*[!@#$&*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{8}$", ErrorMessage = "Password must be strong")` ? – Mou Oct 21 '16 at 11:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126323/discussion-between-stephen-muecke-and-mou). –  Oct 21 '16 at 11:25

0 Answers0