diff --git a/assets/js/checkPay.js b/assets/js/checkPay.js index 9329f06..a1e9567 100644 --- a/assets/js/checkPay.js +++ b/assets/js/checkPay.js @@ -6,6 +6,9 @@ //Expresión regular para validar nombre y apellidos: const regExpr = /^([A-Za-zÁÉÍÓÚñáéíóúÑ]{0}?[A-Za-zÁÉÍÓÚñáéíóúÑ\']+[\s])+([A-Za-zÁÉÍÓÚñáéíóúÑ]{0}?[A-Za-zÁÉÍÓÚñáéíóúÑ\'])+[\s]?([A-Za-zÁÉÍÓÚñáéíóúÑ]{0}?[A-Za-zÁÉÍÓÚñáéíóúÑ\'])?$/g; +//Expresión regular para validar un código promocional: +const refExprCode = /^0?[xX]?[0-9a-fA-F]*$/; + $(document).ready(function() { //Iconos para validar el titular de la tarjeta: @@ -20,6 +23,10 @@ $(document).ready(function() { $("#cvvValid").hide(); $("#cvvInvalid").hide(); + //Iconos para validar el código promocional: + $("#codeValid").hide(); + $("#codeInvalid").hide(); + //Comprueba que el titular de la tarjeta es válido. $("#card-holder").change(function(){ @@ -118,14 +125,46 @@ $(document).ready(function() { $("#cvvInvalid").hide(); cvv[0].setCustomValidity(""); } else { - $("#carcvvValiddNameValid").hide(); + $("#cvvValid").hide(); $("#cvvInvalid").show(); cvv[0].setCustomValidity("El CVV debe tener 3 dígitos."); } }); + //Comprueba el código promocional introducido: + $("#code").change(function(){ + var url = "../assets/php/common/checkPromo.php?code=" + $("#code").val(); + $.get(url, codeCheck); + }); + + //Devuelve true si el nombre y apellidos del titular son válidos, false en caso contrario. function holderCheck(name) { return regExpr.test(name) ? true : false; } -}) \ No newline at end of file + + //Devuelve true si el código promocional es válido, false en caso contrario. + function holderCheck(code) { + return regExprCode.test(code) ? true : false; + } + + //Muestra si el código promocional introducido existe o no. + function codeCheck(data, status) { + const code = $("#code"); + code[0].setCustomValidity(""); + + if(code.val().length === 8 && data === "avaliable"){ + $("#codeValid").show(); + $("#codeInvalid").hide(); + code[0].setCustomValidity(""); + } else if(code.val().length > 0 ){ + $("#codeValid").hide(); + $("#codeInvalid").show(); + code[0].setCustomValidity("El código promocional no es válido."); + } else if(code.val().length === 0 ){ + $("#codeValid").hide(); + $("#codeInvalid").hide(); + code[0].setCustomValidity(""); + } + } +}); \ No newline at end of file diff --git a/assets/php/common/checkPromo.php b/assets/php/common/checkPromo.php new file mode 100644 index 0000000..ab93dde --- /dev/null +++ b/assets/php/common/checkPromo.php @@ -0,0 +1,15 @@ +GetPromotionObj($_GET["code"]); + if ($promo && $promo->getActive()) { + echo "avaliable"; + } + else{ + echo "!avaliable"; + } + } +?> \ No newline at end of file diff --git a/assets/php/includes/promotion_dao.php b/assets/php/includes/promotion_dao.php index 81bba8c..8bcec14 100644 --- a/assets/php/includes/promotion_dao.php +++ b/assets/php/includes/promotion_dao.php @@ -27,7 +27,7 @@ $resul = mysqli_query($this->mysqli, $sql) or die ('Error into query database'); while($fila=$resul->fetch_assoc()){ - $promotions[] = $this->loadPromotion($fila["id"], $fila["tittle"], $fila["description"], $fila["code"], $fila["active"], $fila["img"],); + $promotions[] = $this->loadPromotion($fila["id"], $fila["tittle"], $fila["description"], $fila["code"], $fila["active"], $fila["img"]); } $resul->free(); return $promotions; @@ -76,6 +76,20 @@ return $resul; } + + //Returns a film data . + public function GetPromotionObj($code){ + $sql = sprintf( "SELECT * FROM promotion WHERE promotion.code = '%s'", $code ); + $resul = mysqli_query($this->mysqli, $sql) or die ('Error into query database'); + + $promo = null; + while($fila = $resul->fetch_assoc()){ + $promo = $this->loadPromotion($fila["id"], $fila["tittle"], $fila["description"], $fila["code"], $fila["active"], $fila["img"]); + } + $resul->free(); + + return $promo; + } //Create a new film Data Transfer Object. public function loadPromotion($id, $tittle, $description, $code, $active, $img){ diff --git a/purchase/includes/formPurchase.php b/purchase/includes/formPurchase.php index 6a35c7b..1c05046 100644 --- a/purchase/includes/formPurchase.php +++ b/purchase/includes/formPurchase.php @@ -89,19 +89,16 @@ class FormPurchase extends Form {