HTML Form Validation Using jQuery Source Code

In this tutorial we will create a HTML Form Validation using jQuery. This code will automatically validate the form inputs when the user enter some text. The code use jQuery focusout function to detect if the user enter a text and then validate it with a conditional statement if. This is a user-friendly program feel free to modify it. We will be using jQuery a JavaScript framework that design to simplify HTML DOM tree traversal and manipulation. It can do more in a single line of code than JavaScript can do in a whole function.

Getting Started:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/. And this is the link for the jquery that i used in this tutorial https://jquery.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  5.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">HTML Form Validation Using jQuery Source Code</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-5">
  17.                 <form method="POST" id="myform" action="index.html">
  18.                         <div class="form-group">
  19.                                 <h4>Name</h4>
  20.                                 <input type="text" name="name" id="name" class="form-control">
  21.                                 <span id="name_error" class="text-danger"></span>
  22.                         </div>
  23.                         <div class="form-group">
  24.                                 <h4>Username</h4>
  25.                                 <input type="text" name="username" id="username" class="form-control"/>
  26.                                 <span id="username_error" class="text-danger"></span>
  27.                         </div>
  28.                         <div class="form-group">
  29.                                 <h4>Password</h4>
  30.                                 <input type="password" name="password" id="password" class="form-control"/>
  31.                                 <span id="password_error" class="text-danger"></span>
  32.                         </div>
  33.                         <br />
  34.                         <center><button class="btn btn-primary" style="width:80%;">Submit</button></center>
  35.                 </form>
  36.                 </div>
  37.         </div>
  38. </div>
  39. </body>
  40. <script src="js/jquery-3.2.1.min.js"></script>
  41. <script src="js/script.js"></script>
  42. </html>

Creating the Script

This code contains the script of the application. This code will automatically validate the HTML form when text is entered. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory
  1. $(document).ready(function(){
  2.         $("#name_error").hide();
  3.         $("#username_error").hide();
  4.         $("#password_error").hide();
  5.        
  6.        
  7.         $("#name").focusout(function(){
  8.                 var name = $("#name").val().length;
  9.                
  10.                 if(name < 8 || name > 24 ){
  11.                         $("#name_error").html("Name must be 5 long");
  12.                         $("#name_error").show();
  13.                 }else{
  14.                         $("#name_error").hide();
  15.                 }
  16.         })
  17.        
  18.         $("#username").focusout(function(){
  19.                 var username = $("#username").val().length;
  20.                
  21.                 if(username < 8 || username > 24 ){
  22.                         $("#username_error").html("Username must be 8 - 24 characters");
  23.                         $("#username_error").show();
  24.                 }else{
  25.                         $("#username_error").hide();
  26.                 }
  27.         })
  28.        
  29.         $("#password").focusout(function(){
  30.                 var password = $("#password").val().length;
  31.                
  32.                 if(password < 5){
  33.                         $("#password_error").html("Password too short");
  34.                         $("#password_error").show();
  35.                         $("#password_error").attr('class', "text-warning");
  36.                 }else if(password > 12){
  37.                         $("#password_error").html("Password too long");
  38.                         $("#password_error").show();
  39.                         $("#password_error").attr('class', "text-danger");
  40.                 }else{
  41.                         $("#password_error").hide();
  42.                 }
  43.         })
  44.        
  45.        
  46. });
There you have it we successfully created a HTML Form Validation using jQuery. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment