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
0 answers

Validating Drop Down List in JQuery Mobile

This my attempt at validating a dropdown list. Its actually a flip switch in jquery mobile. I want to force the user to answer "yes" in order to pass validation. I'm trying to figure out a way to do this using the jquery validation plugin. This…
Dave
  • 88
  • 2
  • 7
2
votes
1 answer

jQuery Validate adding accept argument breaks code

i am using jQuery Validate and have looked through this forum for answers but am struggling with one part when adding the accept argument it fails. i have this code $("#addNewDocumentForm").validate({ rules: { inputDocument: { required:…
Dan
  • 1,295
  • 2
  • 22
  • 46
2
votes
1 answer

validating user input inside modal popup using jquery validation plugin

I am trying to validate user input in a jquery UI modal dialog. Jquery validation plugin is used for validation. trying to verify validation in popup button click
2
votes
1 answer

jquery validate plugin override error message for required:

So I have been trying to do global override for all of the fields with a class 'required'. I got it so I can add a red background to any field that fails the validation with the class required like so: $("#signup_form").validate({ errorClass:…
appthat
  • 816
  • 2
  • 10
  • 30
2
votes
1 answer

Bootstrap 3 Signin form and jQuery validation plugin

I use Bootstrap 3 Signin form. But when I add jQuery Validation Plugin it starts to jump. So the problem is: 1. I type Login (correct) 2. Than I click Password to type it, but Password's form is jumping…
condpattern
  • 23
  • 1
  • 1
  • 4
2
votes
1 answer

jquery validate field equalTo element only if another field a certain value

Using jquery validate plugin I'm using the rule equalTo to validate a field as follows: $('#booking').validate({ ignoreTitle: true, rules: { payments: { equalTo: { param: "#sales", depends: function(element) {…
Ray
  • 3,018
  • 8
  • 50
  • 91
2
votes
1 answer

jQuery Validation - Add custom callback function, check if a string has a space doesn't working

This is my code, checkUsername function doesn't working, how should I fixit?
Info
Killer Whale
  • 55
  • 1
  • 8
2
votes
2 answers

Why doesn't jQuery form validation plugin work in my page?

Why does not jQuery form validation plug-in work? This is the code:
Input your own comment:

Steven
  • 24,410
  • 42
  • 108
  • 130
2
votes
3 answers

How to keep jQuery Validation plugin from adding 'error' class to input box

I have the jQuery Validation plugin: //name validation cnForm.validate({ rules: { customerName: { required: true, minlength: 3 }, date: { required: true, dateRegex:…
Joe Essey
  • 3,457
  • 8
  • 40
  • 69
2
votes
3 answers

jQuery Validate plugin: input vs. textarea

I'm wondering why there is a difference in jQuery Validate plugin's default handling of vs.