Advertisement
brilliantmojo

Contact.js

Mar 17th, 2021 (edited)
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var ContactForm;
  2.  
  3. var ContactFormData;
  4.  
  5. function submitForm() {
  6.    
  7.     ContactFormData = new FormData(ContactForm);
  8.  
  9.     ContactFormData.append('ContactsName', $('#ContactsName').val());
  10.     ContactFormData.append('ContactsEmail', $('#ContactsEmail').val());
  11.     ContactFormData.append('ContactsPhone', $('#ContactsPhone').val());
  12.     ContactFormData.append('ContactsCompany', $('#ContactsCompany').val());
  13.     ContactFormData.append('ContactsFile', $('#ContactsFile')[0].files[0]);
  14.     ContactFormData.append('ContactsMessage', $('#ContactsMessage').val());
  15.    
  16.     $('.Contact .LoaderMessage').show();
  17.  
  18.     $.ajax({
  19.  
  20.         url: 'vendors/pages/Contact/sendContact.php',
  21.         type: 'POST',
  22.         data: ContactFormData,
  23.         contentType: false,
  24.         processData: false,
  25.         error:
  26.  
  27.             function(strError) {
  28.                
  29.                 if(strError == 'timeout') {
  30.                    
  31.                     // Do something. Try again perhaps?
  32.                     alert('Seems like there was an error sending your message.');
  33.                
  34.                 }
  35.                
  36.             },
  37.         success:
  38.  
  39.             function(data) {alert(data);
  40.  
  41.                 $('.Contact .LoaderMessage').hide();
  42.  
  43.                 $('.ContactForm input').blur(); // Blur inputs
  44.  
  45.                 $('#formSendStatus').html(data);
  46.                 $('#formSendStatus').fadeIn().delay(2500).fadeOut(); // Show send status for 2.5 seconds
  47.                
  48.                 $('.ContactForm input[type=text], .ContactForm input[type=email], .ContactForm input[type=tel], .ContactForm input[type=file], .ContactForm textarea').val(''); // Resets
  49.  
  50.             },
  51.         timeout: 3000
  52.  
  53.     });
  54.  
  55. }
  56.  
  57. $(document).on('submit', ContactForm, function(event) {
  58.    
  59.     event.preventDefault();
  60.  
  61.     var sendMessage = validateContact();
  62.    
  63.     if(sendMessage) {
  64.         submitForm();
  65.     }
  66.    
  67. });
  68.  
  69. // Check if inputs are appropriately filled
  70. function validateContact() {
  71.  
  72.     var sendMessage = true;
  73.  
  74.     // Name
  75.     if(!$('#ContactsName').val()) {
  76.  
  77.         $('#ContactsName').attr('placeholder', 'Required *');
  78.  
  79.         $('#ContactsName').addClass('ContactFormatError');
  80.  
  81.         $('.ContactButton').addClass('SendMessageFalse');
  82.  
  83.         setTimeout(function() {
  84.  
  85.             $('#ContactsName').attr('placeholder', 'Full Name *');
  86.             $('#ContactsName').removeClass('ContactFormatError');
  87.             $('.ContactButton').removeClass('SendMessageFalse');
  88.  
  89.         }, 1250);
  90.  
  91.         sendMessage = false;
  92.    
  93.     }
  94.  
  95.     // Email
  96.     if(!$('#ContactsEmail').val()) {
  97.  
  98.         $('#ContactsEmail').attr('placeholder', 'Required *');
  99.         $('#ContactsEmail').addClass('ContactFormatError');
  100.         $('.ContactButton').addClass('SendMessageFalse');
  101.  
  102.         setTimeout(function() {
  103.             $('#ContactsEmail').attr('placeholder', 'Email Address *');
  104.             $('#ContactsEmail').removeClass('ContactFormatError');
  105.             $('.ContactButton').removeClass('SendMessageFalse');
  106.         }, 1250);
  107.  
  108.         sendMessage = false;
  109.  
  110.     }
  111.  
  112.     if(!$('#ContactsEmail').val().match(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/)) {
  113.  
  114.         $('#ContactsEmail').val('');
  115.         $('#ContactsEmail').attr('placeholder', 'Incorrect Format *');
  116.         $('#ContactsEmail').addClass('ContactFormatError');
  117.         $('.ContactButton').addClass('SendMessageFalse');
  118.  
  119.         setTimeout(function() {
  120.             $('#ContactsEmail').attr('placeholder', 'Email Address *');
  121.             $('#ContactsEmail').removeClass('ContactFormatError');
  122.             $('.ContactButton').removeClass('SendMessageFalse');
  123.         }, 1250);
  124.  
  125.         sendMessage = false;
  126.  
  127.     }
  128.  
  129.     // Message
  130.     if(!$('#ContactsMessage').val()) {
  131.  
  132.         $('#ContactsMessage').attr('placeholder', 'Required *');
  133.         $('#ContactsMessage').addClass('ContactFormatError');
  134.         $('.ContactButton').addClass('SendMessageFalse');
  135.  
  136.         setTimeout(function() {
  137.             $('#ContactsMessage').attr('placeholder', 'Please include as much information as possible *');
  138.             $('#ContactsMessage').removeClass('ContactFormatError');
  139.             $('.ContactButton').removeClass('SendMessageFalse');
  140.         }, 1250);
  141.  
  142.         sendMessage = false;
  143.  
  144.     }
  145.  
  146.     return sendMessage;
  147.  
  148. }
  149.  
  150. // Delegate keypress function onto #ContactsPhone
  151. $(document).on('keypress', '#ContactsPhone', function(event) {
  152.                    
  153.     // If keypress is not backspace/del and not a key [at all (key code 0)] and less than 0 or greater than 9
  154.     if(event.which != 8 && event.which != 0 && event.which < 48 || event.which > 57) {
  155.        
  156.         $('#ContactsPhone').val('');
  157.         $('#ContactsPhone').attr('placeholder', 'Numbers Only');
  158.         $('#ContactsPhone').addClass('ContactFormatError');
  159.  
  160.         setTimeout(function() {
  161.             $('#ContactsPhone').attr('placeholder', 'Telephone Number');
  162.             $('#ContactsPhone').removeClass('ContactFormatError');
  163.         }, 1250);
  164.  
  165.         return false;
  166.        
  167.     }
  168.    
  169. });
  170.  
  171. // File upload input
  172. $(document).on('change', '#ContactsFile', function(event) {
  173.  
  174.     var file = this.files[0];
  175.  
  176.     var fileType = file.type;
  177.  
  178.     var fileName = $(this).val().split('\\').pop(); // Get the value of the file name and remove the rest of the file path
  179.  
  180.     var fileCount = $(this).get(0).files.length; // Count how many files are selected
  181.  
  182.     var allowedTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/tiff', 'image/tif']; // Make an array of the allowed extentions
  183.    
  184.     // Check if file doesn't have allowed extensions
  185.     if(!((fileType == allowedTypes[0]) || (fileType == allowedTypes[1]) || (fileType == allowedTypes[2]) || (fileType == allowedTypes[3]))) {
  186.        
  187.         $('#ContactFileName').attr('placeholder', 'Images Only');
  188.         $('.ContactFileLabel').addClass('ContactFormatError');
  189.         $('.ContactButton').addClass('SendMessageFalse');
  190.  
  191.         $('#ContactsFile').replaceWith($('#ContactsFile').val('').clone(true)); // Replace the input with an empty input
  192.  
  193.         setTimeout(function() {
  194.             $('#ContactFileName').attr('placeholder', 'No image chosen');
  195.             $('.ContactFileLabel').removeClass('ContactFormatError');
  196.             $('.ContactButton').removeClass('SendMessageFalse');
  197.         }, 1250);
  198.  
  199.     } else {
  200.    
  201.         if(fileCount == 1) {
  202.             $('#ContactFileName').attr('placeholder', fileName);
  203.         } else if(fileCount > 1) {
  204.             $('#ContactFileName').attr('placeholder', fileCount + ' Images');
  205.         } else if(fileCount == 0) {
  206.             $('#ContactFileName').attr('placeholder', 'No image chosen');
  207.         }
  208.  
  209.     }
  210.    
  211. });
  212.  
  213. // Preventing page from redirecting
  214. $(document).on('dragover', 'html', function(event) {
  215.  
  216.     event.preventDefault();
  217.     event.stopPropagation();
  218.  
  219.     $('.ContactFileLabel').css('border', '1px dashed #0006ff');
  220. });
  221.  
  222. $(document).on('drop dragleave', 'html', function(event) {
  223.  
  224.     event.preventDefault();
  225.     event.stopPropagation();
  226.  
  227.     $('.ContactFileLabel').css('border', '1px solid #fff');
  228.  
  229. });
  230.  
  231. // Drag enter
  232. $(document).on('dragenter', '.ContactFileContainer', function(event) {
  233.  
  234.     event.stopPropagation();
  235.     event.preventDefault();
  236.  
  237. });
  238.  
  239. // Drag over
  240. $(document).on('dragover', '.ContactFileContainer', function(event) {
  241.  
  242.     event.stopPropagation();
  243.     event.preventDefault();
  244.  
  245.     $('.ContactFileLabel').css('border', '1px solid #0006ff');
  246.  
  247. });
  248.  
  249. // Drop
  250. $(document).on('drop', '.ContactFileContainer', function(event) {
  251.    
  252.     event.preventDefault();
  253.  
  254.     $('.ContactFileLabel').css('border', '1px solid #fff');
  255.    
  256.     $.each(dataTransfer.files, function(i, file) {
  257.         file.append('ContactsFile', file);
  258.     });
  259.  
  260. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement