From 179c973d2b5cbc721d93d0cce171624c938e88b0 Mon Sep 17 00:00:00 2001 From: IO420 <320154041@pcpuma.acatlan.unam.mx> Date: Wed, 10 Dec 2025 13:20:07 -0600 Subject: [PATCH] added new validation to scanner --- src/components/BarcodeScanner.tsx | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/components/BarcodeScanner.tsx b/src/components/BarcodeScanner.tsx index cdcd05d..c1454bc 100644 --- a/src/components/BarcodeScanner.tsx +++ b/src/components/BarcodeScanner.tsx @@ -9,6 +9,10 @@ interface BarcodeScannerProps { onScan: (code: string) => void; } +function isValidCode(code: string) { + return /^[0-9E]+$/.test(code); +} + export default function BarcodeScanner({ onScan }: BarcodeScannerProps) { const videoRef = useRef(null); const lastScan = useRef(null); @@ -28,7 +32,7 @@ export default function BarcodeScanner({ onScan }: BarcodeScannerProps) { try { stream = await navigator.mediaDevices.getUserMedia({ - video: { facingMode: { ideal: "environment" } }, // 👈 fuerza cámara trasera + video: { facingMode: { ideal: "environment" } }, }); if (!videoRef.current) return; @@ -64,6 +68,13 @@ export default function BarcodeScanner({ onScan }: BarcodeScannerProps) { if (raw && raw !== lastScan.current) { lastScan.current = raw; const processed = raw.startsWith("0") ? raw.slice(1) : raw; + + if (!isValidCode(processed)) { + toast.error("Intente de nuevo."); + lastScan.current = null; + return; + } + onScan(processed); } } @@ -116,7 +127,15 @@ export default function BarcodeScanner({ onScan }: BarcodeScannerProps) { const code = result.getText().trim(); if (code && code !== lastScan.current) { lastScan.current = code; + const processed = code.startsWith("0") ? code.slice(1) : code; + + if (!isValidCode(processed)) { + toast.error("Intente de nuevo"); + lastScan.current = null; + return; + } + onScan(processed); } } else if (err && !(err instanceof NotFoundException)) { @@ -163,4 +182,4 @@ export default function BarcodeScanner({ onScan }: BarcodeScannerProps) { ); } //Chatgpt -//Gemini \ No newline at end of file +//Gemini