1 | (function() { |
---|
2 | /** |
---|
3 | * Función para imprimir los nombres en los label de los input[type=file] |
---|
4 | */ |
---|
5 | var files, fileLabel, signButton, signValidation, formSign; |
---|
6 | |
---|
7 | formSign = document.getElementById('form-sign'); |
---|
8 | files = document.querySelectorAll('.custom-file-input'); |
---|
9 | fileLabel = document.querySelectorAll('.custom-file-input ~ .custom-file-label'); |
---|
10 | signButton = document.getElementById('sign-button'); |
---|
11 | signValidation = [2, 2]; |
---|
12 | |
---|
13 | function validateExtension(name, ext){ |
---|
14 | let buscarElemento = name.indexOf(ext); |
---|
15 | return buscarElemento; |
---|
16 | } |
---|
17 | |
---|
18 | for (var i = 0; i <= files.length -1; i++) { |
---|
19 | files[i].addEventListener('change', function() { |
---|
20 | let backslack = this.value.lastIndexOf("\\") + 1; |
---|
21 | let namePDF = this.value.slice(backslack, this.value.length); |
---|
22 | this.nextElementSibling.innerText = namePDF; |
---|
23 | }); |
---|
24 | } |
---|
25 | |
---|
26 | // Validación que el primer input sea un pdf |
---|
27 | files[0].addEventListener('change', function() { |
---|
28 | let backslack = this.value.lastIndexOf("\\") + 1; |
---|
29 | let namePDF = this.value.slice(backslack, this.value.length); |
---|
30 | let validation = validateExtension(namePDF, "pdf"); |
---|
31 | |
---|
32 | if (validation == -1){ |
---|
33 | this.nextElementSibling.classList.add("border-danger"); |
---|
34 | this.nextElementSibling.classList.add("text-danger"); |
---|
35 | this.nextElementSibling.innerText = namePDF + " no es un archivo PDF"; |
---|
36 | signValidation[0] = 1; |
---|
37 | } else { |
---|
38 | this.nextElementSibling.classList.remove("border-danger"); |
---|
39 | this.nextElementSibling.classList.remove("text-danger"); |
---|
40 | signValidation[0] = 0; |
---|
41 | } |
---|
42 | }); |
---|
43 | |
---|
44 | files[1].addEventListener('change', function() { |
---|
45 | let backslack = this.value.lastIndexOf("\\") + 1; |
---|
46 | let namePDF = this.value.slice(backslack, this.value.length); |
---|
47 | let validation = validateExtension(namePDF, "p12"); |
---|
48 | |
---|
49 | if (validation == -1){ |
---|
50 | this.nextElementSibling.classList.add("border-danger"); |
---|
51 | this.nextElementSibling.classList.add("text-danger"); |
---|
52 | signValidation[1] = 1; |
---|
53 | |
---|
54 | this.nextElementSibling.innerText = namePDF + " no es un archivo p12"; |
---|
55 | } else { |
---|
56 | this.nextElementSibling.classList.remove("border-danger"); |
---|
57 | this.nextElementSibling.classList.remove("text-danger"); |
---|
58 | signValidation[1] = 0; |
---|
59 | } |
---|
60 | |
---|
61 | }); |
---|
62 | // Valida que los campos pdf y p12 tengan archivos válidos |
---|
63 | formSign.addEventListener('submit', function(event){ |
---|
64 | if (signValidation[0] != 0 || signValidation[1] != 0){ |
---|
65 | event.preventDefault(); |
---|
66 | } |
---|
67 | }); |
---|
68 | |
---|
69 | })(); |
---|