Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Not sure how you would inject javascript as you describe, but you may want to consider using the <a href="http://www.tsjensen.com/blog/2010/03/11/ASPNET+MVC+2+MinStringLength+Custom+Validation+Attribute.aspx" rel="nofollow noreferrer">custom validation pattern for ASP.NET MVC 2</a>.</p> <p>Important pieces are the ValidationAttribute, DataAnnotationsModelValidator, registering the validator in Application_Start with DataAnnotationsModelValidatorProvider.RegisterAdapter, and the client side Sys.Mvc.ValidatorRegistry.validators function collection to register your client side validation code. </p> <p>Here's the example code from my post.</p> <pre><code>[RegularExpression("[\\S]{6,}", ErrorMessage = "Must be at least 6 characters.")] public string Password { get; set; } [StringLength(128, ErrorMessage = "Must be under 128 characters.")] [MinStringLength(3, ErrorMessage = "Must be at least 3 characters.")] public string PasswordAnswer { get; set; } public class MinStringLengthAttribute : ValidationAttribute { public int MinLength { get; set; } public MinStringLengthAttribute(int minLength) { MinLength = minLength; } public override bool IsValid(object value) { if (null == value) return true; //not a required validator var len = value.ToString().Length; if (len &lt; MinLength) return false; else return true; } } public class MinStringLengthValidator : DataAnnotationsModelValidator&lt;MinStringLengthAttribute&gt; { int minLength; string message; public MinStringLengthValidator(ModelMetadata metadata, ControllerContext context, MinStringLengthAttribute attribute) : base(metadata, context, attribute) { minLength = attribute.MinLength; message = attribute.ErrorMessage; } public override IEnumerable&lt;ModelClientValidationRule&gt; GetClientValidationRules() { var rule = new ModelClientValidationRule { ErrorMessage = message, ValidationType = "minlen" }; rule.ValidationParameters.Add("min", minLength); return new[] { rule }; } } protected void Application_Start() { RegisterRoutes(RouteTable.Routes); DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(MinStringLengthAttribute), typeof(MinStringLengthValidator)); } Sys.Mvc.ValidatorRegistry.validators["minlen"] = function(rule) { //initialization var minLen = rule.ValidationParameters["min"]; //return validator function return function(value, context) { if (value.length &lt; minLen) return rule.ErrorMessage; else return true; /* success */ }; }; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload