Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In Ui added Scripts jquery.validate.js , jquery.unobtrusive-ajax.min.js , jquery.validate.unobtrusive.min.js
HTML
<script src="/Content/Common/jquery-1.8.3.min.js"></script>
    <script src="/Content/Common/jquery-ui-1.10.1.custom.min.js"></script>
    <script src="/Content/Common/jquery.validate.js" type="text/javascript"></script>
        <script src="/Content/Common/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
    <script src="/Content/Common/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>




C#
public class DateValidationCurrentLessThan : ValidationAttribute, IClientValidatable
   {
       protected override ValidationResult IsValid(object value, ValidationContext validationContext)
       {
           if (value != null)
           {
               var thisDate = (DateTime)value;
               if (thisDate > DateTime.Now)
               {
                   var message = FormatErrorMessage(validationContext.DisplayName);
                   return new ValidationResult(message);
               }
           }
           return ValidationResult.Success;
       }


       public IEnumerable<ModelClientValidationRule>  GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
       {
           ModelClientValidationRule dateGreaterThanRule = new ModelClientValidationRule();
           dateGreaterThanRule.ErrorMessage = FormatErrorMessage(metadata.DisplayName);
           dateGreaterThanRule.ValidationType = "dategreaterthan"; // This is the name the jQuery adapter will use
           //"otherpropertyname" is the name of the jQuery parameter for the adapter, must be LOWERCASE!
           dateGreaterThanRule.ValidationParameters.Add("otherpropertyname", "2");

           yield return dateGreaterThanRule;
       }
   }


but in UT nothing is coming in while rendering

it is not rendering

HTML
data-val-dategreaterthan="The field  is invalid." data-val-dategreaterthan-otherpropertyname="2"
Posted
Updated 19-Nov-15 18:39pm
v2

1 solution

Please check this link
Model Validation Basics in ASP.NET MVC[^]
 
Share this answer
 
Comments
Anil Sharma1983 20-Nov-15 1:05am    
you can add custom method in js file a s given below example
http://www.codeproject.com/Articles/301022/Creating-Custom-Validation-Attribute-in-MVC

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900