All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Form Validation In jQuery

Last Updated : Jul 1, 2023

Form Validation In jQuery

In this article we will show you the solution of form validation in jQuery, there are many benefits to validating forms with a jQuery plugin.

You may apply conditional logic to jQuery form validation and display custom error messages with ease thanks to its new features.

With little to no changes to the markup, a validation library can also assist you in adding validation to your HTML forms.

Additionally, it is simple to add, delete, or change any validity requirements at any time.

Step By Step Guide On Form Validation In jQuery :-

It is typically wise to apply some data validation when including forms for user input on your website.

This not only guarantees that your application receives the right kind of data, but also improves user experience (UX) because users will be alerted to improper input and provided suggestions for the right kind of data.

A client-side validation is one type of validation, whereas a server-side validation is another.

JavaScript is typically used for client side validation, which aims to provide the user with immediate feedback on input problems prior to sending the data to the server.

An additional layer of protection against inaccurate data is provided by server side validation.

Even if you've configured client-side validation, you should still run server-side validation because JavaScript can be overridden.

When verifying a form, client-side form validation is highly helpful and improves the usability of your website.

There are other ways to carry out client-side validation in HTML forms, but the jQuery Validation Plugin is the simplest one.

With a variety of customization options, the jQuery Validation Plugin rapidly implements form validation.

A form is validated using the validate() method depending on the supplied selector.

Custom rules are defined by key-value pairs called rules. The name of the element serves as the key, while the object with rule/parameter pairs or a simple String serves as the value.

Custom messages are defined by key-value pairs called messages. An element's name serves as the key, and the message to be displayed for that element serves as the value.

The validation code for the client side is made simpler by the jQuery plugin.

In addition to an API that allows you to create your own methods, the plugin includes a handy collection of validation techniques, such as URL and email validation.

<html>
<head>
 <meta charset="utf-8">
 <title>Talkerscode Form</title>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
 <script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
 <script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/additional-methods.min.js"></script>
 <script>
  $().ready(function () {
   $("#signupForm").validate({
    rules: {
     fname: "required",
     lname: "required",
     username: {
      required: true,
      minlength: 5
     },
     password: {
      required: true,
      minlength: 7
     },
     confirm_password: {
      required: true,
      minlength: 7,
      equalTo: "#password"
     },
     email: {
      required: true,
      email: true
     },
     agree: "required"
    },
    messages: {
     fname: " Please enter your firstname",
     lname: " Please enter your lastname",
     username: {
      required: " Enter your username here",
      minlength: " Your username must have at least five characters"
     },
     password: {
      required: " Enter your password here",
      minlength: " Your password must contain at least seven characters"
     },
     confirm_password: {
      required: " Please enter a password",
      minlength: " Your password must be consist of at least 7 characters",
      equalTo: " Please enter the same password as above"
     },
     agree: "Please accept our policy"
    }
   });
  });
 </script>
</head>
<body>
 <form class="cmxform" id="signupForm" method="get" action="form-handler.html" autocomplete="off">
  <fieldset>
   <legend>Welcome to Talkerscode</legend>
<p>
    <label for="firstname">Firstname</label>
    <input id="firstname" name="firstname" type="text"></input>
   </p>
<p>
    <label for="lastname">Lastname</label>
    <input id="lastname" name="lastname" type="text"></input>
   </p>
<p>
    <label for="username">Username</label>
    <input id="username" name="username" type="text"></input>
   </p>
<p>
    <label for="password">Password</label>
    <input id="password" name="password" type="password"></input>
   </p>
<p>
    <label for="confirm_password">Confirm password</label>
    <input id="confirm_password" name="confirm_password" type="password"></input>
   </p>
<p>
    <label for="email">Email</label>
    <input id="email" name="email" type="email"></input>
   </p>
<p>
    <label for="agree">Please agree to our policy</label>
    <input id="agree" name="agree" type="checkbox"></input>
   </p>
<p>
    <input class="submit" type="submit" value="submit">
   </p>
  </fieldset>
 </form>
</body>
</html>
  1. We begins our code with the help of HTML & HEAD tags.
  2. Then we add title to our page.
  3. After that we includes the jQuery & jQuery plugin .js files.
  4. Then we start or script.
  5. In script firstly we use ready() method to execute jquery code.
  6. After that we use validate method for validating our form.
  7. Then we create rules which are used to specify all the constraints for the respective fileds.
  8. Then we close our script and starts the body of our program.
  9. We create the form in which we create the fields like firstname, lastname, username, password, confirm_password, email, agree checkbox, submit button.
  10. In the last we end our program with FORM, BODY & HTML tags.

Conclusion :-

As a result, we have successfully learned the jQuery form validation idea. additionally, we discovered how to use a jQuery plugin to advance our form validation.

We have a lot more control over simple HTML form validation when we use simple JavaScript form validation.

For instance, you may simply regulate the appearance of various error messages when an input contains erroneous values.

I hope this article on form validation in jQuery helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪