Dynamic Validation Using Jquery

Introduction

We know that we have two kind of validations; one is client side and another one is at Server side.Here, we will discuss the client side validation, using Jquery, but we will do it dynamically. Now, what do we mean by dynamically? Why we are using this word? Dynamically means that we don't write any specific ID of the control to validate it. We will write a script or a function, using jQuery, which will find the controls within a form and will validate it. Thus, let's swing the bowl.
 
Suppose, you have a form like the image given below and you want to validate this form dynamically. How can you do it? Let's see the form fields first:


I have a form like the image shown above (you can create your own according to  your requirement). Thus, we need to validate this form , using client side validation. Thus, let's start doing this. 
 
First, give a reference of JavaScript library on your page so that we can write jQuery scripting code in own our page.

JavaScript
  1.  <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>  
  2.  <script>  
  3.         function ValidateForm()  
  4.         {  
  5.             var isFormValid = true;  
  6.             $("#tbldynamicform input,select").each(function () {  
  7.                 var FieldId = "span_" + $(this).attr("id");  
  8.                 if ($.trim($(this).val()).length == 0 || $.trim($(this).val())==0) {  
  9.                     $(this).addClass("highlight");  
  10.   
  11.                     //Show required message along with the field  
  12.                     if ($("#" + FieldId).length == 0) {  
  13.                         $("<span class='error' id='" + FieldId + "'>Required</span>").insertAfter(this);  
  14.                     }  
  15.                     //If you fill and again make the field empty in that case again show the message  
  16.                     if ($("#" + FieldId).css('display') == 'none')  
  17.                     {  
  18.                         $("#" + FieldId).fadeIn(500);  
  19.                     }  
  20.                     //$(this).focus();  
  21.                     isFormValid = false;  
  22.                       
  23.                 }  
  24.                 else{  
  25.                     $(this).removeClass("highlight");  
  26.                     if ($("#" + FieldId).length > 0) {  
  27.                         // Hide the message with the fade out effect  
  28.                         $("#" + FieldId).fadeOut(1000);  
  29.                     }  
  30.                 }  
  31.             });  
  32.             return isFormValid;  
  33.         }  
  34.      </script> 
In the code snippet, given above, I gave the reference of Javascript Library and then wrote the code to validate the form. Then write the code for finding controls using one of the selector options we have in Jquery, we give the id of any parent control and on the basis of that we finding all input type. Select controls so I just have input type -- in your case it can be different, you need to modify this according to your requirement.

After finding the control we made a check for length in that control, which means if the text length is greater then zero, we have the text in that input box else we will apply a class over that control and to make it more attractive. I gave the fade in fade out effect on that error message, you can skip this if you don't want to do it or you can apply for jQquery functions to make it more attractive. Now, the function is ready. We just need to call it on submit button, click and we will see the output like the image, given below, if we don't fill in the fields:



That's it, it looks nice doesn't it? Thus, we apply the class over control and in these classes, you can define CSS rules to make it more attractive. If you have good knowledge of CSS, you can apply for amazing styles over it. I just apply following CSS over this.

CSS
  1. <style>  
  2.         td{padding3px;}  
  3.        .highlight{border1px solid red !important;}  
  4.        .error {positionrelative;background#d5bb73;border4px solid #d5bb73;margin:11px;}  
  5.        .error:after, .error:before {right: 100%;top: 50%;bordersolid transparent;content" ";height0;width0;positionabsolute;pointer-events: none;}  
  6.        .error:after {border-color: rgba(21366300);border-right-color#d5bb73;border-width12px;margin-top-14px;}  
  7.        input[type="text"]{width:200pxheight:21px;}  
  8.        select{width:204pxheight:25px;}  
  9. </style> 
So that's it for now, enjoy the coding. "Life begins at the end of your comfort zone ;)"