# Check number format

<input id="user-number" type="number" />
<output id="formatted" />
<script>
  const output = document.getElementById('formatted')
  document
    .getElementById('user-number')
    .addEventListener('input', (e) => {
      const chars = String(e.target.value)
        .split('')
        .reverse()
        .map((c, k) => (k > 0 && k % 3 === 0 ? c + ',' : c))
        .reverse()
        .join('')
      output.innerText = chars
    })
</script>
cy.get('#user-number').type('1234567')

How would we check the formatted output? We could say that the formatted output should match a regular expression.

The regular expression can lead an error though. For example, the same regular expression matches incorrectly formatted number. For example, the program could incorrectly set the output to "000" and it would still work!

Since we know the number, simply check for the expected string without any regular expression logic in the spec.

cy.contains('#formatted', '1,234,567')