2026-11-06 · Q&A guide

Check Lottery Winning Numbers in JavaScript with AlaSQL and Excel

Learn how to read an Excel sheet in the browser using AlaSQL and compare it with numbers entered in an HTML form.

Load the Excel workbook with AlaSQL

AlaSQL can read XLSX files directly in the browser. Fetch the file as an ArrayBuffer, then pass it to `alasql.promise` with the `SELECT * FROM XLSX(?)` syntax. The promise resolves to an array of rows, each row being a plain JavaScript object whose keys are the column letters (e.g., C, D, E, F, H, I).

fetch('results.xlsx')
  .then(r => r.arrayBuffer())
  .then(buf => alasql.promise('SELECT * FROM XLSX(?)', [buf]))
  .then(rows => window.lotteryRows = rows) // store globally for later use
  .catch(err => console.error('Failed to load Excel:', err));

Map the relevant columns to a simple array

The raw rows contain many columns, but you only need C, D, E, F, H, and I. Transform each row into an array of six numbers so the comparison logic stays fast and readable.

function normalizeRows(rows) {
  return rows.map(r => [
    Number(r.C), // first number
    Number(r.D), // second number
    Number(r.E), // third number
    Number(r.F), // fourth number
    Number(r.H), // fifth number
    Number(r.I)  // sixth number
  ]);
}
// Call after the Excel file is loaded
window.winningSets = normalizeRows(window.lotteryRows || []);

Read user input and test for a match

When the form is submitted, collect the six values, convert them to numbers, and compare the resulting array with each winning set. A strict equality check on each position guarantees an exact match.

function checkIfYouHaveWinningNumber() {
  const form = document.forms[0];
  const userSet = [
    Number(form.first.value),
    Number(form.second.value),
    Number(form.third.value),
    Number(form.fourth.value),
    Number(form.fifth.value),
    Number(form.sixth.value)
  ];

  const isWinner = window.winningSets.some(set =>
    set.length === userSet.length && set.every((v, i) => v === userSet[i])
  );

  alert(isWinner ? `Congratulations – you won! ${userSet.join(', ')}` : `Sorry, you lose. ${userSet.join(', ')}`);
}
// Attach to the form in HTML: onsubmit="checkIfYouHaveWinningNumber(); return false;"

Full working example

Below is a minimal HTML page that ties everything together. The script loads the Excel file once, prepares the winning sets, and runs the check on every submit.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Lottery Checker</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/alasql/0.4.2/alasql.min.js"></script>
  <script>
    // 1️⃣ Load Excel
    fetch('results.xlsx')
      .then(r => r.arrayBuffer())
      .then(buf => alasql.promise('SELECT * FROM XLSX(?)', [buf]))
      .then(rows => {
        window.winningSets = rows.map(r => [
          Number(r.C), Number(r.D), Number(r.E), Number(r.F), Number(r.H), Number(r.I)
        ]);
      })
      .catch(err => console.error(err));

    // 2️⃣ Form handler
    function checkIfYouHaveWinningNumber() {
      const f = document.forms[0];
      const user = [Number(f.first.value), Number(f.second.value), Number(f.third.value), Number(f.fourth.value), Number(f.fifth.value), Number(f.sixth.value)];
      const win = (window.winningSets || []).some(set => set.every((v,i) => v===user[i]));
      alert(win ? `You win! ${user.join(', ')}` : `You lose. ${user.join(', ')}`);
    }
  </script>
</head>
<body>
  <form onsubmit="checkIfYouHaveWinningNumber(); return false;">
    <input type="number" name="first" min="0" max="100" required>
    <input type="number" name="second" min="0" max="100" required>
    <input type="number" name="third" min="0" max="100" required>
    <input type="number" name="fourth" min="0" max="100" required>
    <input type="number" name="fifth" min="0" max="100" required>
    <input type="number" name="sixth" min="0" max="100" required>
    <button type="submit">Check</button>
  </form>
</body>
</html>

Performance tips & common pitfalls

• Load the Excel file only once; repeated fetches waste bandwidth. • Ensure column letters in the query match the exact layout of your sheet (A‑Z, then AA, etc.). • Convert every cell to `Number` before comparison; Excel may store numeric strings. • If the workbook is large, consider indexing the winning sets with a `Set` of JSON strings for O(1) lookup.

Takeaway: Use AlaSQL to read Excel in the browser, normalize the needed columns, and compare them with form input for instant lottery validation.

People also ask

Can I read a local Excel file without a server?

Yes, use an `<input type="file">` element, read the file with `FileReader` as an ArrayBuffer, then feed it to AlaSQL.

What if the winning numbers are in non‑contiguous columns?

Map the required column letters (e.g., C, D, E, F, H, I) manually in the `normalizeRows` function as shown.

Is AlaSQL the only library for this task?

No, alternatives include SheetJS (xlsx) or PapaParse for CSV, but AlaSQL offers SQL‑like queries directly on XLSX data.

Inspired by a public discussion on Stack Overflow. This article is an original explanation for learners.

← All posts