I am trying to pass the value of a variable as the value of a cookie, but I only get the value to be the name of the variable.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
var code = '';
let btn = document.getElementById('btnEnvio'); // boton envio
function captcha() {
var alpha = new Array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
var i;
for (i = 0; i < 8; i++) {
var a = alpha[Math.floor(Math.random() * alpha.length)];
var b = alpha[Math.floor(Math.random() * alpha.length)];
var c = alpha[Math.floor(Math.random() * alpha.length)];
var d = alpha[Math.floor(Math.random() * alpha.length)];
var e = alpha[Math.floor(Math.random() * alpha.length)];
var f = alpha[Math.floor(Math.random() * alpha.length)];
var g = alpha[Math.floor(Math.random() * alpha.length)];
}
code = a + ' ' + b + ' ' + c + ' ' + d + ' ' + e + ' ' + f + ' ' + g;
crearCookie('cogido', code, 120);
creaIMG(code);
}
function validCaptcha(txtInput) {
var string1 = removeSpaces(leerCookie('codigo'));
var string2 = removeSpaces(document.getElementById(txtInput).value);
if (string1 === string2) {
document.getElementById("btnEnvio").disabled = false;
return true;
}
else {
captcha();
return false;
}
}
function removeSpaces(string) {
return string.split(' ').join('');
}
function creaIMG(texto) {
var ctxCanvas = document.getElementById('captcha').getContext('2d');
var fontSize = "30px";
var fontFamily = "Arial";
var width = 250;
var height = 50;
//tamaño
ctxCanvas.canvas.width = width;
ctxCanvas.canvas.height = height;
//color de fondo
ctxCanvas.fillStyle = "#fff";
ctxCanvas.fillRect(0, 0, width, height);
//puntos de distorsión
ctxCanvas.setLineDash([7, 10]);
ctxCanvas.lineDashOffset = 5;
ctxCanvas.beginPath();
var line;
for (var i = 0; i < (width); i++) {
line = i * 5;
ctxCanvas.moveTo(line, 0);
ctxCanvas.lineTo(0, line);
}
ctxCanvas.stroke();
//formato texto
ctxCanvas.direction = 'ltr';
ctxCanvas.font = fontSize + " " + fontFamily;
//texto posicion
var x = (width / 9);
var y = (height / 3) * 2;
//color del borde del texto
ctxCanvas.strokeStyle = "yellow";
ctxCanvas.strokeText(texto, x, y);
//color del texto
ctxCanvas.fillStyle = "red";
ctxCanvas.fillText(texto, x, y);
}
// Funcion para crear una cookies
function crearCookie(nombre, valor, exsegun) {
var d = new Date();
console.log(valor);
d.setTime(d.getTime() + (exsegun));
var expires = "expires="+ d.toUTCString();
document.cookie = nombre + "=" + valor + ";" + expires;
}
// Funcion para leer una cookies
function leerCookie(nombre) {
var name = nombre + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
I need the value of the code variable, which is a captcha that is generated, as I say, I only get the value to be code, therefore the comparison always returns false and I don't enter. Thank you