fm-ezra6-1

Complete Form Validation JavaScript Part 4

Apr 30th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     //function checks for empty fields
  2.     function CheckEmpty(field_id){
  3.         //
  4.         var MyValue = document.getElementById(field_id);
  5.         if(MyValue.value == "" || MyValue.value == null){
  6.             //code
  7.             displayWarning(field_id, MyValue.parentNode.id, "You can't leave this empty.");
  8.         }
  9.     }
  10.    
  11.     //function validates the given email id
  12.     function ValidateEmail(email){
  13.         var MyEmail = document.getElementById(email);
  14.         var format = /.+@.+/;
  15.         if(!MyEmail.value.match(format)){
  16.             displayWarning(email, MyEmail.parentNode.id, "Please enter a valid email.");
  17.         };
  18.     }
  19.    
  20.     //function validates the password field
  21.     function ValidatePassword(password){
  22.         var MyPassword=document.getElementById(password);
  23.         var format = /(?=^.{8,}$)(?=.*\d)(?=.*[!@#$%^&*]+)(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/;
  24.         if(!MyPassword.value.match(format)){
  25.             displayWarning(password, MyPassword.parentNode.id, "The password must be a combination of Uppercase, Lowercase, Numbers, Special characters and at least 8 characters long.");
  26.         }
  27.     }
  28.    
  29.     //function confirms that passwords are the same
  30.     function ConfirmPassword(password, confirm_password){
  31.         var Password=document.getElementById(password);
  32.         var ConfirmPassword = document.getElementById(confirm_password);
  33.         if(ConfirmPassword.value !== Password.value){
  34.             displayWarning(confirm_password, ConfirmPassword.parentNode.id, "Please enter the same password in both fields.");
  35.         }
  36.     }
  37.    
  38.     //function checks the checkboxes
  39.     function ValidateCheckbox(checkbox){
  40.         var check_box = document.getElementById(checkbox);
  41.         if(check_box.checked == false){
  42.             //code
  43.             displayWarning(checkbox, check_box.parentNode.parentNode.id, "You must agree to proceed.");
  44.         }
  45.     }
  46.    
  47.     //function validates the selections
  48.     function ValidateSelect(select){
  49.         //code
  50.         selected = document.getElementById(select);
  51.         if(selected.selectedIndex == 0){
  52.             //code
  53.             displayWarning(select, selected.parentNode.id, "Please select your country.");
  54.         }
  55.     }
  56.    
  57.     //function displays the error message
  58.     function displayWarning(field_id, parent_div, message){
  59.         var x = document.getElementById(parent_div).querySelectorAll(".warning_msg").length;
  60.         if(x === 0 ){
  61.             var para = document.createElement("p");
  62.             para.setAttribute("class","warning_msg");
  63.             var error_msg = document.createTextNode(message);
  64.             para.appendChild(error_msg);
  65.             document.getElementById(parent_div).appendChild(para);
  66.             document.getElementById(field_id).className += " warning_brd";
  67.         };
  68.        
  69.        
  70.     }
Advertisement
Add Comment
Please, Sign In to add comment