---
title: "How to include jQuery validation in a web page?"  
description: "How to include jQuery validation in a web page?"  
author: "Utpal Vishwas"  
published: 2023-05-11  
updated: 2023-05-11  
canonical: https://www.mindstick.com/forum/158280/how-to-include-jquery-validation-in-a-web-page  
category: "jquery"  
tags: ["jquery validate", "html form", "form validation"]  
reading_time: 3 minutes  

---

# How to include jQuery validation in a web page?

How to include [jQuery validation](https://www.mindstick.com/forum/158292/how-do-you-validate-multiple-form-fields-at-once-using-jquery-validation) in a [web page](https://www.mindstick.com/forum/718/sql-server-report-alignment-to-center-of-web-page)?

## Replies

### Reply by Aryan Kumar

To include jQuery [validation](https://www.mindstick.com/articles/12234/validation-using-data-annotation-using-entity-framework) in a [web](https://www.mindstick.com/articles/12783/the-ultimate-bunch-of-free-web-design-resources) [page](https://www.mindstick.com/articles/13031/why-to-make-a-wikipedia-page), you will need to:

- **Include the jQuery library.** The jQuery library is a JavaScript library that provides a variety of useful functions, including form validation. To include the jQuery library in your HTML document, add the following code to the head of your document:

Code snippet

```plaintext
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
```

- **Create a form.** The form is the element that will be validated. To create a form, add the following code to your HTML document:

Code snippet

```plaintext
<form id="myForm">
  <input type="text" name="username" />
  <input type="password" name="password" />
  <input type="submit" value="Submit" />
</form>
```

- **Attach the jQuery validation plugin to the form.** The jQuery validation plugin is a plugin that provides a variety of form validation functions. To attach the jQuery validation plugin to the form, add the following code to your JavaScript file:

Code snippet

```plaintext
$(document).ready(function() {
  $("#myForm").validate();
});
```

- **Define the validation rules.** The validation rules are the rules that will be used to validate the form fields. To define the validation rules, add the following code to your JavaScript file:

Code snippet

```plaintext
$(document).ready(function() {
  $("#myForm").validate({
    rules: {
      username: {
        required: true
      },
      password: {
        required: true,
        minlength: 6
      }
    }
  });
});
```

- **Validate the form.** The form can be validated by calling the validate() method on the form object. For example, the following code would validate the form:

Code snippet

```plaintext
$("#myForm").validate();
```

If the form is valid, the submit() method on the form object will be called. If the form is invalid, an error message will be displayed.

Here is an example of a complete HTML and JavaScript file that includes jQuery validation:

HTML

```plaintext
<!DOCTYPE html>
<html>
<head>
  <title>jQuery Validation Example</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
  <form id="myForm">
    <input type="text" name="username" />
    <input type="password" name="password" />
    <input type="submit" value="Submit" />
  </form>
  <script>
    $(document).ready(function() {
      $("#myForm").validate({
        rules: {
          username: {
            required: true
          },
          password: {
            required: true,
            minlength: 6
          }
        }
      });
    });
  </script>
</body>
</html>
```

This code will create a form with two fields, a username field and a password field. The form will be validated when the user clicks the submit button. If the form is valid, the submit button will be clicked and the form will be submitted. If the form is invalid, an error message will be displayed.


---

Original Source: https://www.mindstick.com/forum/158280/how-to-include-jquery-validation-in-a-web-page

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
