MaxLength Attribute not generating client-side validation attributes

前端 未结 11 517
北荒
北荒 2020-11-30 02:41

I have a curious problem with ASP.NET MVC3 client-side validation. I have the following class:

public class Instrument : BaseObject
{
    public int Id { get         


        
相关标签:
11条回答
  • 2020-11-30 03:10

    This can replace the MaxLength and the MinLength

    [StringLength(40, MinimumLength = 10 , ErrorMessage = "Name cannot be longer than 40 characters and less than 10")]
    
    0 讨论(0)
  • 2020-11-30 03:11

    I tried this for all the inputs in my html document(textarea,inputs,etc) that had the data-val-length-max property and it works correctly.

    $(document).ready(function () {
        $(":input[data-val-length-max]").each(function (index, element) {
            var length = parseInt($(this).attr("data-val-length-max"));
            $(this).prop("maxlength", length);
        });
    });
    
    0 讨论(0)
  • 2020-11-30 03:14

    Props to @Nick-Harrison for his answer:

    $("input[data-val-length-max]").each(function (index, element) {
    var length = parseInt($(this).attr("data-val-length-max"));
    $(this).prop("maxlength", length);
    });
    

    I was wondering what the parseInt() is for there? I've simplified it to this with no problems...

    $("input[data-val-length-max]").each(function (index, element) {
        element.setAttribute("maxlength", element.getAttribute("data-val-length-max"))
    });
    

    I would have commented on Nicks answer but don't have enough rep yet.

    0 讨论(0)
  • 2020-11-30 03:16

    I had this same problem and I was able to solve it by implementing the IValidatableObject interface in my view model.

    public class RegisterViewModel : IValidatableObject
    {
        /// <summary>
        /// Error message for Minimum password
        /// </summary>
        public static string PasswordLengthErrorMessage => $"The password must be at least {PasswordMinimumLength} characters";
    
        /// <summary>
        /// Minimum acceptable password length
        /// </summary>
        public const int PasswordMinimumLength = 8;
    
        /// <summary>
        /// Gets or sets the password provided by the user.
        /// </summary>
        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }
    
        /// <summary>
        /// Only need to validate the minimum length
        /// </summary>
        /// <param name="validationContext">ValidationContext, ignored</param>
        /// <returns>List of validation errors</returns>
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            var errorList = new List<ValidationResult>();
            if ((Password?.Length ?? 0 ) < PasswordMinimumLength)
            {
                errorList.Add(new ValidationResult(PasswordLengthErrorMessage, new List<string>() {"Password"}));
            }
            return errorList;
        }
    }
    

    The markup in the Razor is then...

    <div class="form-group">
        @Html.LabelFor(m => m.Password)
        @Html.PasswordFor(m => m.Password, new { @class = "form-control input-lg" }
        <div class="password-helper">Must contain: 8 characters, 1 upper-case, 1 lower-case
        </div>
        @Html.ValidationMessagesFor(m => m.Password, new { @class = "text-danger" })
    </div>
    

    This works really well. If I attempt to use [StringLength] instead then the rendered HTML is just not correct. The validation should render as:

    <span class="text-danger field-validation-invalid field-validation-error" data-valmsg-for="Password" data-valmsg-replace="true"><span id="Password-error" class="">The Password should be a minimum of 8 characters long.</span></span>
    

    With the StringLengthAttribute the rendered HTML shows as a ValidationSummary which is not correct. The funny thing is that when the validator fails the submit is still blocked!

    0 讨论(0)
  • 2020-11-30 03:20

    I just used a snippet of jquery to solve this problem.

    $("input[data-val-length-max]").each(function (index, element) {
       var length = parseInt($(this).attr("data-val-length-max"));
       $(this).prop("maxlength", length);
    });
    

    The selector finds all of the elements that have a data-val-length-max attribute set. This is the attribute that the StringLength validation attribute will set.

    The each loop loops through these matches and will parse out the value for this attribute and assign it to the mxlength property that should have been set.

    Just add this to you document ready function and you are good to go.

    0 讨论(0)
  • 2020-11-30 03:21

    MaxLengthAttribute is working since MVC 5.1 update: change notes

    0 讨论(0)
提交回复
热议问题