javascript - MVC 5 - Validate a specific field on client-side -


i want populate city/state drop down list based on postal code user types textbox. when text changes, i'm going make ajax call retrieve data. however, want perform ajax request valid postal codes. field validates using dataannotations.regularexpression attribute , jquery.validate.unobtrusive validation library. i'm unclear on can , can't used jquery.validate when using unobtrusive. i've looked @ unobtrusive code, haven't gotten understanding of yet. 2 questions:

using javascript,

  1. is there way force validation on specific field, not whole form?
  2. is there way check whether specific field valid?

after digging around in source code, i've come these conclusions. first, purpose of unobtrusive wire rules , messages, defined data- attributes on form elements mvc, jquery.validation. it's configuring/wiring validation, not complete wrapper around it, when comes performing validation set up, don't have worry "circumventing", or not involving, unobtrusive.

so answer questions:

  1. yes, there 2 ways. validator.element(element) function , $.valid() extension method. $.valid calls validator.element internally. difference $.valid works on jquery allows perform validation on 1 or more fields (or form itself). validator.element performs validation on single element , requires have instance of validator object. although documentation states .validate() "validates selected form", appears initialize validation form, , if has been called, returns validator form. here examples of 2 ways validate input:

  2. yes, not without performing validation. both of methods #1 return boolean can use them determine whether field valid. unfortunately there doesn't appear exposed library allows check validation without, in effect, showing or hiding validation message. have @ , run rule(s) field code, may possible, need didn't justify spending time on it.

example:

<form>     <input id="txtdemo" type="text"></input> </form> ...     <script type="text/javascript">     $("#txtdemo").valid();      //or      //get form makes sense (probably not this)     var validator = $("form").validate();      //note: while .element can accept selector,      //it work on first item matching selector.     validator.element("#txtdemo"); </script> 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -