3

On one of the view model fields, I have set the min length in ASP.NET MVC Data Annotation attribute.

[MinLength(5, ErrorMessage = "A minimum of 5 digits is required")]

Based on a dropdown selection(which has 2 values), MinLength needs to be updated to 10. ClientSideValidation is enable and so validation needs to reflect the changes before it is posted back to the controller.

Tried changing the attribute "data-val-minlength-min" using jQuery based on dropdown value, but it did not change the validation.

Any suggestions please.

8
  • The html5 data attribute for validation is ONLY for client-side validation. The MinLength attribute is used for server-side validation (and to generate the html5 data atribute). Commented Jan 6, 2017 at 9:55
  • @ADreNaLiNe-DJ, How do I update it to some other value on a drop down change. Commented Jan 6, 2017 at 9:57
  • Try by creating a custom attribute by inheriting MinLength where the passed value is a default value and when your custom attribute is called by server-side validation, you look in the http request to get the value returned by the dropdown. I don't know if it works, but it's a thing i would try if i had this need. Commented Jan 6, 2017 at 9:59
  • @ADreNaLiNe-DJ, I am having issues with that because the dropdown change does not do any server action. Also the main reason, is the submit button does not allow me to submit to the server as there is client side validation enabled. Commented Jan 6, 2017 at 11:25
  • 1
    Create you own conditional validation attribute so that you get both client and server side validation - The Complete Guide To Validation In ASP.NET MVC 3 - Part 2 Commented Jan 7, 2017 at 9:39

2 Answers 2

1

Try this approach. This is to use RemoteValidations for more than one fields in the model.

[Remote("CheckForSelectedDropDownLengthMethod","ControllerName",AdditionalFields="SelectedDropDownName",ErrorMessage="The length should be be 10 characters.")]

public class ControllerName: Controller
{
    public JsonResult CheckForSelectedDropDownLengthMethod(Model yourmodel)
        {
              // write your logic to validate the logic here ,
              // Get the selected value of the dropdown, and the field where you want to check the length.
        }
}

Please follow one of the example https://www.codeproject.com/Tips/669824/Implementing-Remote-Validation-in-MVC

Sign up to request clarification or add additional context in comments.

1 Comment

This one as the name suggests does the validation remotely so it takes time. I have other validations also in my form and since ClientValidation is enabled, all the other inputs get validated soon. It looks a little odd when only one of them takes some time. I have upvoted a comment from @StephenMuecke. That looks like something I need. I am already half way through that implementation.
0

@StephenMuecke gave me the solution I needed. It works on client side and server side as desired. I am posting it as answer so that the next person can easily see it.

Create you own conditional validation attribute so that you get both client and server side validation - The Complete Guide To Validation In ASP.NET MVC 3 - Part 2

Comments

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.