Open In App

Form validation using jQuery Poppa Plugin

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to implement form validation using jQuery Poppa plugin. It is a very easy to use, reliable, cross-browser friendly, and lightweight plugin that makes client-side validation very simple.

Note: Please download the jQuery Poppa plugin in the working folder and include the required files in the head section of your HTML code. 

<link href=”https://fonts.googleapis.com/css?family=Roboto:300,400,700” 
rel=”stylesheet” type=”text/css”/>
<link href=”https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css” 
rel=”stylesheet” type=”text/css”/>
<link href=”https://use.fontawesome.com/releases/v5.5.0/css/all.css” 
rel=”stylesheet” type=”text/css”/>
<link href=”validation.css?family=Roboto:300,400,700” 
rel=”stylesheet” type=”text/css”/>
<link href=”global.css?family=Roboto:300,400,700” 
rel=”stylesheet” type=”text/css”/>
<link href=”github.css?family=Roboto:300,400,700” 
rel=”stylesheet” type=”text/css”/>
<script src=”global.js”></script> 
<script src=”poppa.js”></script> 
<script src=”highlight.js”></script>

Example 1: The following example demonstrates simple form validation using jQuery Poppa plugin. The output shows the validation in action when given invalid inputs.

HTML




<!DOCTYPE html>
<html>
<head>
  <meta name="viewport"
        content="width=device-width, initial-scale=1">
  <title>jQuery form validation Poppa plugin</title>
  
  <link rel="stylesheet"
        href=
  
  <!-- Include Bootstrap CSS -->
  <link rel="stylesheet"
        href=
  
  <!-- Include FontAwesome CSS -->
  <link rel="stylesheet"
        href=
  
  <!-- Include the Poppa Plugin Stylesheets -->
  <link rel="stylesheet" type="text/css" 
        href="validation.css">
  <link rel="stylesheet" type="text/css"
        href="global.css">
  <link rel="stylesheet" type="text/css" 
        href="github.css">
  <style>
    h2 {
      padding-left: 300px;
    }
  </style>
</head>
  
<body>
  <h2 style="color:green">
    GeeksforGeeks
  </h2>
  <b style="padding-left:300px">
    jQuery form validation Poppa
    plugin
  </b>
  <br><br>
  <div class="container">
    <div class="row">
      <div class="col-9">
        <div class="example-block mb-4">
          <form action="" class="user-registration mb-3">
            <h6 class="title mb-2 mt-0">
              Registration form for User
            </h6>
            <div class="form-group">
              <label>Login ID</label>
              <input name="login" type="text"
                     data-validation-length="min:3" 
                     class="form-control" required>
            </div>
            <div class="form-group">
              <label>Email ID</label>
              <input name="email" type="email"
                     class="form-control" required>
            </div>
            <div class="form-group">
              <label>Password</label>
                
              <input name="password" type="password"
                     class="form-control" required>
            </div>
            <div class="form-group">
              <label>Website</label>
              <input name="website" data-validation-type="url"
                     class="form-control">
            </div>
            <div class="form-group">
              <label class="mb-0">
                I agree to the terms of service
              </label>
              <input type="checkbox" 
                     data-validation-message=
                     "You have to agree to terms of service"
                     required>
            </div>
            <button type="submit" class="btn btn-primary">
              Submit
            </button>
          </form>
        </div>
      </div>
    </div>
  </div>
  
  <!-- Include jQuery -->
  <script src=
  </script>
  
  <!-- Include the Poppa Plugin Scripts -->
  <script src="global.js"></script>
  <script src="poppa.js"></script>
  <script src="highlight.js"></script>
  <script>
    $(document).ready(function () {
  
      /** Initiate highlighting */
      hljs.initHighlightingOnLoad();
  
      /** Initiate validation on each form */
      $('.user-registration').validation({
        'autocomplete': 'off',
        'liveValidation': false
      });
    });
  </script>
</body>
</html>


 
Output:

Before Validation:

After Validation:

Enter some invalid input to check the validation done by the plugin.
 

Example 2: The following example demonstrates the above example with active live validation with some minor custom changes. When live validation is enabled, it allows each input to be validated immediately. This happens when the user types into the input or when it loses focus.

html




<!DOCTYPE html>
<html>
<head>
  <meta name="viewport"
        content="width=device-width,
                 initial-scale=1">
  <title>jQuery form validation Poppa plugin</title>
  
  <link rel="stylesheet" 
        href=
  
  <!-- Include Bootstrap CSS -->
  <link rel="stylesheet"
        href=
  
  <!-- Include FontAwesome CSS -->
  <link rel="stylesheet"
        href=
  
  <!-- Include the Poppa Plugin Stylesheets -->
  <link rel="stylesheet" type="text/css" href="validation.css">
  <link rel="stylesheet" type="text/css" href="global.css">
  <link rel="stylesheet" type="text/css" href="github.css">
  <style>
    h2 {
      padding-left: 300px;
    }
  </style>
</head>
  
<body>
  <h2 style="color:green">
    GeeksforGeeks
  </h2>
  <b style="padding-left:300px">
    jQuery form validation Poppa plugin 
  </b>
    
<p></p>
  
  <div class="container">
    <div class="row">
      <div class="col-9">
        <div class="example-block mb-4">
          <form action="" class="live-validation mb-3" 
                id="live-validation">
            <div class="form-group">
              <label>Login ID</label>
  
              <!-- Validation for Upper case only-->
              <input name="login" type="text" 
                     data-validation-regexp="^[A-Z]+$" 
                     class="form-control" required>
            </div>
            <div class="form-group">
              <label>Salary</label>
  
              <!-- Validation for type number 
              with min and max values-->
              <input name="login" type="number"
                     min="10000" max="30000" 
                     class="form-control" required>
            </div>
            <div class="form-group">
              <label>Email ID</label>
              <input name="email" type="email"
                     class="form-control" required>
            </div>
            <div class="form-group">
              <label>Password</label>
              <input name="password"
                     data-validation-type="alphanumeric" 
                     type="password" class="form-control"
                     required>
            </div>
            <div class="form-group">
              <label>Website</label>
  
              <!-- Valid url patterns include 
                www.sitename.com or
                 https://www.sitename.com -->
              <input name="website"
                     data-validation-type="url" 
                     class="form-control">
            </div>
            <div class="form-group">
              <label>Describe yourself</label>
              <textarea name="bio"
                data-validation-length="min:10,max:150"
                data-validation-hint="Write your biodata"
                data-validation-hint="Describe about yourself"
                class="form-control">
              </textarea>
            </div>
            <div class="form-group">
              <label class="mb-0">
                I agree to the terms of service
              </label>
              <!-- Custom validation message -->
              <input type="checkbox" 
                     data-validation-rqmessage=
                     "Please agree to terms of service"
                     required>
            </div>
            <button type="submit" class="btn btn-primary">
              Submit Form
            </button>
          </form>
        </div>
      </div>
    </div>
  </div>
  
  <!-- Include jQuery -->
  <script src=
  </script>
  
  <!-- Include the Poppa Plugin Scripts -->
  <script src="global.js"></script>
  <script src="poppa.js"></script>
  <script src="highlight.js"></script>
  <script>
    $(document).ready(function () {
  
      /* Initiate highlighting */
      hljs.initHighlightingOnLoad();
  
      /* Initiate live validation on each form */
      $('.live-validation').validation({
        'autocomplete': 'on',
        'liveValidation': true
      });
    });
  </script>
</body>
</html>


Output:



Last Updated : 19 Aug, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads