Zephyrnet Logo

How to Set Up Basic jQuery Form Validation in Two Minutes

Date:

This tutorial shows you how to set up a basic form validation with jQuery, demonstrated by a registration form.

We’re going to use the jQuery Validation Plugin to validate our form. The basic principle of this plugin is to specify validation rules and error messages for HTML elements in JavaScript.

Here’s a live demo of what we’re going to build:

See the Pen
jQuery Form Validation
by SitePoint (@SitePoint)
on CodePen.

Step 1: Include jQuery

First, we need to include the jQuery library. The jQuery validation plugin has been tested up to jQuery version 3.1.1, but the demo in this article works perfectly with version 3.4.1, which is the latest one.

You can use any of the following download options:

  • Download it from jquery.com
  • Download it using Bower: $ bower install jquery
  • Download it using npm or Yarn: $ npm install jquery or yarn add jquery
  • Use a CDN: https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js

Create a new HTML file named index.html and include jQuery before the closing </body> tag:

<!-- Change the "src" attribute according to your installation path -->
<script src="vendor/jquery/dist/jquery.min.js"></script>

If you’d like to use Bower or npm but aren’t familiar with them, you might be interested in these two articles:

Step 2: Include the jQuery Validation Plugin

Choose between:

  • Download it from the plugin’s github repo
  • Download it using Bower: $ bower install jquery-validation
  • Download it using npm: npm i jquery-validation
  • NuGet: Install-Package jQuery.Validation
  • Use a CDN: https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js

Include the plugin after jQuery:

<!-- Change the "src" attribute according to your installation path -->
<script src="vendor/jquery-validation/dist/jquery.validate.min.js"></script>

Step 3: Create the HTML Form

For the registration, we want to collect the following user information:

  1. first name
  2. last name
  3. email
  4. password

So, let’s create our form containing these input fields:

<div class="container"> <h2>Registration</h2> <form action="" name="registration"> <label for="firstname">First Name</label> <input type="text" name="firstname" id="firstname" placeholder="John"/> <label for="lastname">Last Name</label> <input type="text" name="lastname" id="lastname" placeholder="Doe"/> <label for="email">Email</label> <input type="email" name="email" id="email" placeholder="john@doe.com"/> <label for="password">Password</label> <input type="password" name="password" id="password" placeholder="●●●●●"/> <button type="submit">Register</button> </form>
</div>

When integrating this into a real application, don’t forget to fill in the action attribute, to make sure the form is submitted to the correct destination.

Step 4: Create Styles for the Form

Create a new file, css/styles.css, and include it in the <head> section of your HTML file:

<link rel="stylesheet" href="css/style.css"/>

Copy the following styles into the newly created file:

@import url("https://fonts.googleapis.com/css?family=Open+Sans"); /* Styles */
* { margin: 0; padding: 0;
} body { font-family: "Open Sans"; font-size: 14px;
} .container { width: 500px; margin: 25px auto;
} form { padding: 20px; background: #2c3e50; color: #fff; -moz-border-radius: 4px; -webkit-border-radius: 4px; border-radius: 4px;
}
form label,
form input,
form button { border: 0; margin-bottom: 3px; display: block; width: 100%;
}
form input { height: 25px; line-height: 25px; background: #fff; color: #000; padding: 0 6px; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box;
}
form button { height: 30px; line-height: 30px; background: #e67e22; color: #fff; margin-top: 10px; cursor: pointer;
}
form .error { color: #ff0000;
}

Note the styles for .error, which will be used for validation error messages.

Step 5: Create the Validation Rules

Finally, we need to initialize the form validation plugin. Create a new file js/form-validation.js and reference it after the <script> tag of the jQuery Validation plugin:

<script src="js/form-validation.js"></script>

Copy the following code into the newly created file:

// Wait for the DOM to be ready
$(function() { // Initialize form validation on the registration form. // It has the name attribute "registration" $("form[name='registration']").validate({ // Specify validation rules rules: { // The key name on the left side is the name attribute // of an input field. Validation rules are defined // on the right side firstname: "required", lastname: "required", email: { required: true, // Specify that email should be validated // by the built-in "email" rule email: true }, password: { required: true, minlength: 5 } }, // Specify validation error messages messages: { firstname: "Please enter your firstname", lastname: "Please enter your lastname", password: { required: "Please provide a password", minlength: "Your password must be at least 5 characters long" }, email: "Please enter a valid email address" }, // Make sure the form is submitted to the destination defined // in the "action" attribute of the form when valid submitHandler: function(form) { form.submit(); } });
});

Conclusion

That’s it, you’re done! Now you have an idea how to set up form validation with jQuery. Please keep in mind that this doesn’t replace server-side validation. It’s still possible for a malicious user to manipulate or bypass the validation rules (for example, by using the browser’s developer tools).

Source: https://www.sitepoint.com/basic-jquery-form-validation-tutorial/?utm_source=rss

spot_img

Latest Intelligence

spot_img