6

I want to populate a city/state drop down list based on the postal code a user types into a textbox. So when the text changes, I'm going to make an ajax call to retrieve the data. However, I only want to perform that ajax request for valid postal codes. The field already validates using the DataAnnotations.RegularExpression attribute and jquery.validate.unobtrusive validation library. I'm unclear on what can and can't be used from jquery.validate when using unobtrusive. I've looked at the unobtrusive code, but haven't gotten an understanding of it yet. So two questions:

Using javascript,

  1. is there a way to force validation on a specific field, not the whole form?
  2. is there a way to check whether a specific field is valid?
xr280xr
  • 12,621
  • 7
  • 81
  • 125

3 Answers3

9

After digging around in the source code, I've come to these conclusions. First, the purpose of unobtrusive is to wire up the rules and messages, defined as data- attributes on the form elements by MVC, to jQuery.validation. It's for configuring/wiring up validation, not a complete wrapper around it, so when it comes to performing validation that is already set up, you don't have to worry about "circumventing", or not involving, unobtrusive.

So to answer the questions:

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

  2. Yes, but not without also performing the validation. Both of the methods from #1 return a boolean so you can use them to determine whether a field is valid. Unfortunately there doesn't appear to be anything exposed by the library that allows you to check the validation without, in effect, showing or hiding the validation message. You would have to get at and run the rule(s) for the field from your code, which may be possible, but my need didn't justify spending the time on it.

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>
xr280xr
  • 12,621
  • 7
  • 81
  • 125
  • Ok, but how to make it display the error message that would be normally displayed after form validation? – ed22 Apr 28 '22 at 10:22
6

you can find if a single field is valid and trigger this validation this way: $("#myform").validate().element("#elem1");

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

  • It says validate() will validate the form. I want to validate only a specific field. $("#myField").valid() seems to tell me if it's valid, but I don't see where that's documented. Is it dependable? – xr280xr Jun 19 '15 at 20:48
  • I think this is what you are looking for [link](http://stackoverflow.com/questions/2738925/jquery-validate-plugin-trigger-validation-of-single-field) – Feder Catalin Jun 19 '15 at 21:13
-2
Use like this:

$('#Create').on('click', function () {
  var form = $('#test').closest('form');

  $(form).validate();

  if (!$(form).valid()) {
    return
  } else {
    // Bide the data 
  }
});

Hope it works for you

Abhi
  • 4,123
  • 6
  • 45
  • 77
Manohar Thakur
  • 313
  • 1
  • 2
  • 9