Advertisement
YoCTG

js code

Feb 16th, 2021
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* function to test the length       */
  2. function testLength(value, length) {
  3.  
  4.     if(value.length == length) {
  5.        
  6.         return true;
  7.     }
  8.         return false;
  9. }
  10.  
  11. /* check if the value passed is a real value */
  12. function testNumber(value) {
  13.    
  14.     if(isNaN(value)){
  15.         return false;
  16.     }
  17.         return true;
  18.     }
  19.    
  20. /* check if the control's value is the correct length */
  21. function validateControl(control, name, length){
  22.    
  23.     // the result of this function
  24.     var result = false;
  25.     // test the number
  26.     if(testLength(control.value,length)){
  27.         // test the number
  28.         if(testNumber(control.value)){
  29.            
  30.             result = true;
  31.         }
  32.     }
  33.             return result;
  34. }
  35.    
  36. /* checks if a credit card is valid or not */
  37. function validateCreditCard(value){
  38.    
  39.     value = value.split(" ").join(" ");
  40.     // the result of the function
  41.     var result = true;
  42.     // the test for it being a #
  43.     if(!testNumber(value)){
  44.        
  45.         console.log("Credit card details entered are not a number");
  46.         return false;
  47.     }
  48.     if(!(Number(value.charAt(0))== 3 || Number(value.charAt(0)) == 4 || Number(value.charAt(0)) == 5 || Number(value.charAt(0)) == 6)){
  49.        
  50.     Console.log("Invalid Credit card starting with" + Number(value.charAt(0)));
  51.     // if the credit card is invalid
  52.     return false;
  53.     }
  54.    
  55.     /* if the credit card is valid then check for the length of the digits */
  56.     if(Number(value.charAt(0) == 3)) { // this is for the AmEx
  57.        
  58.         if(!testLength(value,15)){
  59.            
  60.             console.log("Invalid Credit Card");
  61.             return false;
  62.         }
  63.         }
  64.         else{
  65.             if(!testLength(value,16)){
  66.                
  67.                 console.log("Invalid Credit Card");
  68.                 return false;
  69.             }
  70.         }
  71.         return result;
  72. }
  73.  
  74. /* if the value is greater than today's date or not ) */
  75. function validateDate(value){
  76.    
  77.     var todayDate = new Date().getDate();
  78.    
  79.     if(Number(value.split("-")[2])>todayDate){
  80.        
  81.         console.log("Wrong Date Selcected");
  82.         return false;
  83.     }
  84.         // the date must be valid
  85.         return true;
  86. }
  87.    
  88. /* test that the email is in the correct format w/ RegEx */
  89. function validateEmail(value){
  90.    
  91.     var RegEx = /\S+@\S+\.\S+/;
  92.     return RegEx.test(value);
  93. }
  94.  
  95. /* validate the form */
  96. function validateForm()
  97. {
  98.    
  99.     // we need to get all of the references of dom objects in vars
  100.     var cvc = document.getElementByld("cvc");
  101.     var zip = document.getElementByld("Zip");
  102.     var cc = document.getElementByld("CreditCard");
  103.     var date = document.getElementByld("Date");
  104.     var state = document.querySelector("#State");
  105.     var email = document.getElementByld("Email");
  106.    
  107.     // check all details entered in the form, is the cvc 3 digits and is the zip 5 digits.
  108.     if (validateControl(cvc, "cvc", 3) && validateControl(zip, "Zip", 5) && validateCreditCard(cc.value) && validateDate(date.value) && validateEmail(email.value) && validateState(state.selectedIndex))
  109.     {
  110.        
  111.         console.log("Payment Submitted");
  112.         document.querySelector('.output').textContent = "Payment Sent";
  113.         return true;
  114.     }
  115.         document.querySelector('.output').textContent = "Payment Failed";
  116.         return false;
  117. }
  118.  
  119. /* the state selected is checked if its valid */
  120. function validateState(value){
  121.    
  122.     var state = document.getElementById("State");
  123.     if(value != state.selectedIndex || state.selectedIndex == 0) {
  124.        
  125.         console.log("Please select the correct state");
  126.         return false;
  127.     }
  128.         return true;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement