fm-ezra6-1

Complete Form Validation JavaScript Part 2

Apr 30th, 2018
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.20 KB | None | 0 0
  1. <!--------Video Link--------->
  2. <!--https://www.youtube.com/watch?v=GMzszNc-xuI&index=2&list=PLFmBk1phabmF_gAOuhq6AI6Zn-iSmFe9x-->
  3.  
  4.  
  5. <!-------------HTML------------->
  6.     <div class="form-group">
  7.         <label for="name" class="col-sm-2 col-lg-2 control-label">Name:</label>
  8.         <div class="col-sm-10 col-lg-5" id="name_div">
  9.             <input type="text" class="form-control" id="name" placeholder="Full Name" onblur="CheckEmpty('name')"/>
  10.         </div>
  11.     </div>
  12.  
  13.  
  14. <!-----Modified Javascript function------>
  15. <script>
  16.     function CheckEmpty(field_id){
  17.         //
  18.         var MyValue = document.getElementById(field_id);
  19.         if(MyValue.value == "" || MyValue.value == null){
  20.             //code
  21.             displayWarning(field_id, MyValue.parentNode.id, "You can't leave this empty.");
  22.         }
  23.     }
  24. </script>
  25.  
  26.  
  27. <!--------New Javascript Code--------->
  28. <script>
  29.     //function displays the error message
  30.     function displayWarning(field_id, parent_div, message){
  31.         var para = document.createElement("p");
  32.         var error_msg = document.createTextNode(message);
  33.         para.appendChild(error_msg);
  34.         document.getElementById(parent_div).appendChild(para);
  35.     }
  36. </script>
Advertisement
Add Comment
Please, Sign In to add comment