Javascript - Simple jQuery Form Validation

In this tutorial we will create a Simple jQuery Form Validation using Javascript. This code can be used in validating some forms to prevent errors. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is a text-based programming language meant to run as part of a web-based application. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding.

Before we started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the bootstrap that has been used for the layouthttps://getbootstrap.com/ And this is the link for the jQuery https://jquery.com/

The Main Interface

This code contains the interface of the application. This code will render application and display the form that will be use in vakidation. 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">Javascript - Simple jQuery Form Validation</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <form method="POST" id="myform" action="index.html">
  17.                         <div class="form-inline">
  18.                                 <h4>Name</h4>
  19.                                 <input type="text" name="name" id="name" class="form-control" style="width:50%;"/>
  20.                                 <span id="name_error" class="text-danger"></span>
  21.                         </div>
  22.                         <div class="form-inline">
  23.                                 <h4>Username</h4>
  24.                                 <input type="text" name="username" id="username" class="form-control" style="width:50%;"/>
  25.                                 <span id="username_error" class="text-danger"></span>
  26.                         </div>
  27.                         <div class="form-inline">
  28.                                 <h4>Password</h4>
  29.                                 <input type="password" name="password" id="password" class="form-control" style="width:50%;"/>
  30.                                 <span id="password_error" class="text-danger"></span>
  31.                         </div>
  32.                         <br />
  33.                         <center><button class="btn btn-primary" style="width:80%;">Submit</button></center>
  34.                 </form>
  35.         </div>
  36. </div>
  37. </body>
  38. <script src="js/jquery-3.2.1.js"></script>
  39. <script src="js/script.js"></script>
  40. </html>

Creating the Script

This code contains the script of the application. This script will prompt the a validation to confirm the form value. To do this just copy write the code as shown below inside the text editor and save it as script.js.
  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. });
There you have it we successfully created a Simple jQuery Form Validation using Javascript. 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