How to implement form validation in javascript/jquery

How to implement form validation in javascript/jquery

You landed on right page, Here I will teach you an easy way to implement form validation in javascript/jquery. Before we start I am hoping that you know well how to make form in html right? if yes then lets start what you need to make form validation. There are many ways to validate a form but here we are going to discuss only two main and super duper easy methods.

  1. How to validate using HTML – Super easy
  2. Using jQuery validating our form

How to implement form validation in HTML

I know you will skip this part but still i want to make you familiar with what is html :

HTML stands for Hyper Text Markup Language and it is the standard markup language for creating Web pages. It describes the structure of a Web page/Web Pages. The HTML consists of a series of elements and these HTML.

Now HTML has inbuilt feature for forms to validate itself by not adding any extra hustle. Its super duper easy . You just need to make the input fields required. Let me show you with an easy example.

Requirements to implement form validation in HTML:

  • Bootstrap css for designing but still optional if you choose other methods to design you website.

I love using bootstrap it saves lot of time. Here is the html code to add form validation.

How to implement form validation using jQuery

Now let talk about with more advance robust feature of JavaScript/jQuery to validate html form.

Requirements to implement form validation in jQuery:

  1. jQuery main js file
  2. jQuery validation js file

Its your choice to use cdn link for these files or to download files to your server. Here I’m giving you cdn links for the files

https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js

https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.5/jquery.validate.min.js

Steps :

  1. Include these two files in header
  2. Create a form and assign it an ID.
  3. in <script></script> tag use this id to validate your form fields.
  4. in head tag add a class to show error messages in red color->
     .error{
          color: red;
        }
  5. Please follow the below example i have given with the form.

Leave a Comment