Questions tagged [jquery-validate]

The jQuery Validate plugin is a jQuery plugin by Jörn Zaefferer. Its purpose is to perform client-side form validation of user entered data.

The jQuery Validate plugin is a plugin by Jörn Zaefferer. Its purpose is to perform client-side form validation of user entered data.

Helpful Links:


Stack Snippet Starter Pack:

HTML - Include the plugin script someplace after the jQuery library:
(Use CDN links or host the scripts yourself)

<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.14.0/jquery.validate.js"></script>

<form id="myform" action="post.php">
  <input type="text" name="first_name" /><br/>
  <input type="text" name="last_name" /><br/>
  <input type="text" name="phone" /><br/>
  <input type="submit" />
</form>
$(document).ready(function() {  // <-- ensure form's HTML is ready

  $("#myform").validate({  // <-- initialize plugin on the form.
    // your rules and other options,
    rules: {
      first_name: {  // <-- this is the name attribute, NOT id
        required: true
      },
      last_name: {
        required: true
      },
      phone: {
        required: true,
        digits: true
      }
    }
  });

});

jsFiddle Demo: http://jsfiddle.net/2nhcfkLj/

Documented Options: http://jqueryvalidation.org/validate


Helpful questions:

Other typical issues:

  • All input elements to be validated must be enclosed within a set of <form></form> tags. The only elements that can be validated are select, textarea, certain input types, and certain elements containing the contenteditable attribute.
  • Rules are defined by input name attributes, not by id, when declared within the rules option of .validate().
  • all input elements to be validated must contain a unique name attribute. (All radio or checkbox elements within a single "grouping" may share the same name as this one grouping is considered a single data point. However, each grouping must contain a unique name.)
  • .validate() should be called once within DOM ready to initialize the plugin. Optionally use .valid() to test the form for validity and get a boolean result of that test.
  • There is no need to enclose .validate() inside of any click or submit handler. The plugin will automatically capture and handle the submit button.
  • A name with certain special characters must be enclosed in quotes when declared within the rules option of .validate().
  • Use the submitHandler callback function to deal with successfully validated forms and/or submit via ajax.
  • Use the invalidHandler callback function for invalid forms.
  • If using the highlight or unhighlight callback function, be sure to also include the other one. They are complementary and should be used together for best results.
  • By default, the plugin will ignore any hidden input elements. This can be prevented by setting the ignore option to ignore: [] (ignore nothing; validate everything).
  • If you have multiple submit buttons where one (such as "save") needs to bypass validation but still needs to submit the form data, use class="cancel" on the button.

Related tags

6769 questions
2
votes
1 answer

unobtrusive validation with custom validator that requires ajax post

I'm using ASP.NET MVC with unobtrusive validation. I need to add a new custom validation attribute with client-side validation, which is fine (I've already got some of those defined). The problem is that in the client side validation, I need to make…
Matt Roberts
  • 26,371
  • 31
  • 103
  • 180
2
votes
1 answer

How to override your own jQuery's validate method?

I use jQuery validation method and I've set some default settings such as below $.validator.setDefaults({ errorContainer : "#msgErrors ul", errorLabelContainer: "#msgErrors", wrapper: "li", submitHandler: function(form) { …
Philippe Gioseffi
  • 1,488
  • 3
  • 24
  • 41
2
votes
0 answers

How to JSON Serialize the MVC ModelState object for jQuery Validation consumption?

I'm unable to figure out the correct JSON object I need to pass into jQuery validation's showErrors() method. So on the client I have JavaScript: var validator = $("form").validate(); validator.showErrors(modelstate_json); And server-side…
puri
  • 1,829
  • 5
  • 23
  • 42
2
votes
2 answers

Validation based on field id rather than field name with jQuery Validation plugin

By default jQuery validation plugin validates form fields based on name of the field. I want the plugin need validate based on field id. http://bassistance.de/2013/03/22/release-validation-plugin-1-11-1/
muni
  • 1,362
  • 2
  • 19
  • 21
2
votes
3 answers

Do I need to use name attributes when validating a bootstrap form with jQuery Validation plugin?

Still kind of new and I just started using bootstrap but basically when using stock HTML I see name attributes being used like so but with…
nope
  • 177
  • 1
  • 4
  • 16
2
votes
1 answer

digits rule of JQuery.validate not working on numeric input

I've got a form, within an input Html element: and its 'type' attribute is set to "number" because I want to take advantage of HTML5's features. In my js code I initialize jQuery.validate…
andrea.rinaldi
  • 1,167
  • 1
  • 13
  • 33
2
votes
2 answers

TypeError for jquery validation script

I want to try some simple example with jquery validate plugin in mvc application. I have a JS module with this method: ValidateRestriction: function (element) { var inputs = $('form').validator(); inputs.data("validator").checkValidity(); …
user2598794
  • 697
  • 2
  • 10
  • 23
2
votes
2 answers

Using jQuery .ajax during an unbtrusive adapter for validation

I have an unobtrusive validation defined on a class which validates that the number entered is a valid number in the database. I'd prefer not to use a Remote attribute, as it ties me to a specific controller instead of a service, and the class is…
tlbignerd
  • 1,104
  • 9
  • 21
2
votes
2 answers

jQuery Validate not binding, with no errors

I have a simple form with jQuery Validate and I have initialized the jQuery Validate plugin and setup my rules and messages, but for some reason it's still firing the form submit, and refreshing the page, without validating the form. I have the…
Carl Weis
  • 79
  • 7
2
votes
1 answer

how to exclude special characters from regex?

I'm a very rookie with regexs, but this is very simple and i can't understand why it allows special characters like quotation marks or dots before the string when i use them in jquery validation plugin for validating a form. I need the input to be 6…
GabAntonelli
  • 757
  • 2
  • 9
  • 26
2
votes
1 answer

Removing a validation message set with showErrors()

Using the showErrors() function of JQuery validation, I am manually adding errors to my form: var errors = {}; errors['Field1'] = 'Field1 has an error'; errors['Field2'] = 'Field2 has an error'; errors['Field3'] = 'Field3 has an…
Jonathan
  • 13,947
  • 17
  • 94
  • 123
2
votes
1 answer

Form validation, form.valid() works but not form.validate()

I am having a form in my page and I am using the validation jquery plugin from here http://jqueryvalidation.org/ My form is:
novellino
  • 1,069
  • 4
  • 22
  • 51
2
votes
1 answer

jQuery Validation Submits non-ajax even when fails

i am using jQuery validate , and it actually works , meaning it shows errors and asks user to fix them , though if the user clicks on submit - it submits with no problems. Can someone please help me ? Code samples are below :
Rodion
  • 21
  • 2
2
votes
2 answers

jQuery Validation plugin not removing error class

I'm stuggling with the removeClass from a jQuery function. I'm a newbie when it comes to jQuery and can't really think of an answer after a lot of search and testing. I'm working with bootstrap. So, here's my HTML.
Ariel
  • 1,507
  • 2
  • 20
  • 28
2
votes
2 answers

JS Form validate and send form without submit input

I've tried to send my form without input[submit] but its not working properly. Tutorial Address : Link I have used jquery validation plugin. I put image button instead of input[submit]. And its not working. I want to assing my image to the validate…
Levent Kaya
  • 49
  • 10