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

jQuery Validation ignore: ":hidden" not always working

I have function to validation some special forms but I'm not able to exclude hidden elements function specialFormValidation() { $("#specialForm").validate({ ignore: ':hidden', errorPlacement: function(sError, oElement) { …
LJ Wadowski
  • 6,424
  • 11
  • 43
  • 76
2
votes
1 answer

validating the checkbox in jquery with the minimum limit

How to validate the checkboxes in jquery with the minimum limit of selection. i am using the jquery plugin for form validation that validates the field by passing the field name. jQuery('#myform').validate({ rules: { …
Mehar
  • 2,158
  • 3
  • 23
  • 46
2
votes
1 answer

Jquery validate doesn't add success class

i have a problem with jquery validate (v. 1.11.1) that is driving me insane... I have the following contact form (i'm using bootstrap):
gfabi
  • 96
  • 3
  • 9
2
votes
1 answer

Jquery Validation for Dynamically Created Fields

I have basic javascript code to generate input text areas as below $("#btnAdd").click(function (e) { var itemIndex = $("#container input.iHidden").length; e.preventDefault(); var newItem = $("
umki
  • 769
  • 13
  • 31
2
votes
2 answers

Jquery Validation Plugin, dynamic form validation

I'm using the Jquery Validation Plugin to forms loaded via Ajax (dynamic forms). I know that as of Jquery 1.4, live events on submit is now possible. Now the problem is I want to show a confirm message after the dynamic form has been validated. My…
codegy
  • 2,259
  • 4
  • 25
  • 24
2
votes
2 answers

Regex invalid group error while using in javascript

I have the following regex that checks for multiple types of email address inputs [\W"]*(?.*?)[\"]*?\s*[<(]?(?\S+@[^\s>)]+)[>)]? I got this off How do I regex a name and an email out of the 3 major email clients in ruby? The problem i…
Derek
  • 450
  • 5
  • 9
2
votes
3 answers

How to get unobtrusive client-side validation on a Required List<> of objects in MVC?

I have a model in which an employee contains a list of functions. The employee should at least have one function. public class Employee { [Required(ErrorMessage = "Name is Required")] public string Name { get; set; } …
comecme
  • 6,086
  • 10
  • 39
  • 67
2
votes
2 answers

Jquery Validation - clear error message box when input has been corrected

I'm using the jquery validation plus the plugin from bassistance.de, the validation errors are being shown in a specific area of the screen and not besides the invalid form element this is achieved by specifying an empty errorPlacement function in…
Tom
  • 84
  • 1
  • 1
  • 5
2
votes
3 answers

Jquery input form default value validation

I have a small submit form on the sidebar of http://wines.effectwave.com/ I am trying to validate it with Jquery. I want to check if someone submits the form with the default values (eg. 'Full Name') then it should raise an error. So far I have…
2
votes
1 answer

combining custom error placement with success is not working jquery validate plugin

I tried and want to bring the success message as in this demo (iam using bootstrap2) http://alittlecode.com/files/jQuery-Validate-Demo/index.html However, since iam using errorPlacement function for placing my error messages like this in …
Learner_Programmer
  • 1,259
  • 1
  • 13
  • 38
2
votes
1 answer

select2 validation - Select a minimum of one value

I am using select2 and jQuery Validation plugins. HTML for select2:

select2…
spyder
  • 330
  • 3
  • 6
  • 18
2
votes
1 answer

Jquery validator weird behaviour with remote

I have a registration form which is using jQuery validator as: $('reg_form').validate({ ignore:"", rules : { user_name : { required :1, minlength :8, remote :"check_user_name.php" }, password : { required :1, …
user3228222
2
votes
2 answers

Jquery Validation, Show Custom message with value from id

I Use jquery.validate.js for field validation. Its display error messages as per declare in .js file for appropriate field. I use this code for custom validation for email field.