-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
109 lines (84 loc) · 3.08 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* Llaves utilizadas de encriptado
e= enter
i= imes
a= ai
o= ober
u= ufat
*/
// Declarar Variables---
var input_encriptar = document.querySelector("#input-encriptar");
var encriptarBtn = document.querySelector("#encriptar-btn");
var desencriptarBtn = document.querySelector("#desencriptar-btn");
var answer = document.querySelector("#answer");
var copyBtn = document.querySelector("#copyBtn");
const error_message = document.querySelector("#error_message"); // Error message
// Crear el escuchador del evento btn encriptar
encriptarBtn.addEventListener("click", function (event) {
event.preventDefault();
let inputEncriptar = document.getElementById("input-encriptar").value;
document.getElementById("input-encriptar").value = '';
encriptandoMsg(inputEncriptar);
});
// Crear el escuchador del evento btn desencriptar
desencriptarBtn.addEventListener("click", function (event){
event.preventDefault();
let inputEncriptar= document.getElementById('input-encriptar').value;
desencriptandoMsg(inputEncriptar);
});
// Crear el escuchador del evento btn copiar
copyBtn.addEventListener("click", function (event) {
event.preventDefault();
rowscopy();
document.getElementById("answer").value = '';
});
// Funcion para el encriptado
function encriptandoMsg (inputEncriptar){
let encriptar = inputEncriptar.replace (/e/g, 'enter').replace(/i/g, 'imes').replace(/a/g, 'ai').replace(/o/g, 'ober').replace(/u/g, 'ufat');
document.getElementById("answer").value = encriptar
}
// funcion boton copiar
function rowscopy() {
let inputEncriptar = document.getElementById('answer');
inputEncriptar.select();
document.execCommand('copy');
}
// Funcion para e desencriptado
function desencriptandoMsg (inputEncriptar){
let deseencriptar = inputEncriptar.replace(/enter/g, 'e').replace(/imes/g, 'i').replace(/ai/gi, 'a').replace(/ober/g, 'o').replace(/ufat/g, 'u');
document.getElementById("answer").value = deseencriptar
}
//validar caracteres
const validateInput = (input_encriptar) => {
let regex = /[A-ZÁÉÍÓÚáéíóúñÑ]/g;
let valor;
if (regex.test(input_encriptar) || input_encriptar.length == 0) {
valor = true;
} else {
valor = false;
}
if (valor && input_encriptar.length > 0) {
showErrorMessage(
"¡ALERTA!! No se permite acentos ni caracteres especiales, el texto debe ir en minúsculas"
);
} else if (input_encriptar.length==0 || input_encriptar.replace(" ", "").length == 0) {
showErrorMessage("Ingresar el texto");
}else {
hideErrorMessage();
}
return valor;
};
input_encriptar.addEventListener("keyup", () => {
if (input_encriptar.value.length > 0) {
validateInput(input_encriptar.value);
} else {
hideErrorMessage();
}
});
const showErrorMessage = (input_encriptar) => {
// Mostrar mensaje del error
error_message.style.display = "block";
error_message.innerHTML = input_encriptar;
};
const hideErrorMessage = () => {
error_message.style.display = "none";
};