MVC 5 - checking a specific field on the client side

I want to populate a city / state drop-down list based on the zip code that the user types in the text box. So when the text changes, I'm going to make an ajax call to retrieve the data. However, I only want to fulfill this ajax request for valid postal codes. The field already validates the use of the DataAnnotations.RegularExpression attribute and the jquery.validate.unobtrusive validation library. I do not understand what you can and cannot use from jquery.validate when using unobtrusive ones. I looked at the unobtrusive code, but have not yet understood it. So, two questions:

Using javascript,

  • Is there a way to force validation on a specific field, and not on the entire form?
  • Is there any way to check if a certain field is really?
+5
source share
3 answers

Rummaging around in the source code, I came to these conclusions. First, the goal of the unobtrusive is to associate rules and messages defined as data- attributes in MVC form elements with jQuery.validation. It is intended to configure / enable validation, and not to completely wrap around it, so when it comes to performing validation that is already configured, you don’t need to worry about a “workaround” or without involving an unobtrusive one.

So, to answer the questions:

  1. Yes, there are two ways. Validator.element(element) function and extension method $.fn.valid() . .valid actually calls Validator.element inside. The difference is that .valid works with jQuery, which allows you to perform validation on one or more fields (or on the form itself). Validator.element only validates one item and requires that you have an instance of a validator object. Although the documentation states that .validate () "validates the selected form", in fact, it apparently initiates validation of the form, and if it has already been called, it simply returns a validator for the form. So, here are examples of two ways to validate input (below # 2).

  2. Yes, but not without verification. Both methods from # 1 return a boolean, so you can use them to determine the validity of a field. Unfortunately, the library does not provide anything that would allow you to verify validation without actually showing or hiding the validation message. You would have to get and run the rule (s) for the field from your code, which may be possible, but my need did not justify spending time on this.

Example:

 <form> <input id="txtDemo" type="text"></input> </form> ... <script type="text/javascript"> $("#txtDemo").valid(); //or //Get the form however it makes sense (probably not like this) var validator = $("form").validate(); //Note: while .element can accept a selector, //it will only work on the first item matching the selector. validator.element("#txtDemo"); </script> 
+8
source

you can find out if one field is valid and run this check as follows: $("#myform").validate().element("#elem1");

more details here http://docs.jquery.com/Plugins/Validation/Validator/element#element

+5
source
 Use like this: $('#Create').on('click', function () { var form = $('#test').closest('form'); $(form).validate(); if (!$(form).valid()) { return } else { // Bide the data } }); 

Hope this works for you.

-2
source

All Articles