Start with a clear rule
Suppose you ask someone how many tickets they want. A usable answer is a positive whole number: 1, 2, or 12, for example. A blank answer, 0, a negative number, or a decimal does not meet that rule. Before writing a condition, decide what other input you will accept. In this example, spaces around the number and leading zeros are allowed, so " 03 " becomes 3. Letters and signs such as + or - are not allowed.
Input from a browser prompt or a text field is usually text, even when it looks numeric. Converting it immediately can hide important differences: Number("") returns 0, and Number("2.5") returns 2.5. Neither result tells you whether the original text followed your rule. The safer sequence is to check the text, convert it, and then check the resulting number.
Build the validator
You can try the following example in the console of a modern browser that supports ES2015 JavaScript, or with Node.js 20 or later. No package is needed. Define the function as follows: function parseQuantity(raw) { if (typeof raw !== "string") return null; const text = raw.trim(); if (!/^[0-9]+$/.test(text)) return null; const value = Number(text); if (!Number.isSafeInteger(value) || value < 1) return null; return value; }
The function returns a number when the input passes every check and null otherwise. The type check rejects values such as null, which a canceled browser prompt can produce. Trimming removes surrounding whitespace. The pattern /^[0-9]+$/ requires one or more digits from start to finish, so it rejects empty text, decimal points, signs, and letters. Only then does Number(text) perform the conversion.
Check the number after conversion
A string of digits is not automatically a suitable JavaScript number. JavaScript numbers cannot represent every very large integer exactly. Number.isSafeInteger(value) checks that the converted result is an integer within the range where integer values can be represented reliably. The separate value < 1 check enforces the positive-count rule. For instance, "0" contains only digits and converts cleanly, but it still fails because a ticket count must be at least 1.
Try a few calls to confirm the behavior: parseQuantity(" 12 ") returns 12, parseQuantity("003") returns 3, and parseQuantity("0") returns null. So do parseQuantity("2.5"), parseQuantity("-4"), parseQuantity("five"), and parseQuantity(""). Testing both accepted and rejected examples helps you catch a missing check before the function becomes part of a larger program.
Use the result without hiding failure
A caller can check the result explicitly: const quantity = parseQuantity("3"); if (quantity === null) { console.log("Enter a positive whole number."); } else { console.log("Quantity:", quantity); } Avoid a check such as if (quantity) when the return contract might later change to allow zero. Comparing with null says precisely what you mean: validation failed.
Keep the validation function separate from the message shown to the user. That lets the same function work with a prompt, a form, or a set of test inputs without changing its rules. The caller can decide whether to show an error, ask again, or leave the field unchanged. After practicing, you can browse Chamber Music Companion for a change of subject before returning to the exercise.
Adjust the rule deliberately
This function is for counts, not every kind of numeric input. If zero is valid in your program, change the lower-bound check. If decimals are valid, the digit-only pattern is too restrictive; you will need to define which decimal formats you accept before converting them. If leading zeros should be rejected, add a check for those before calling Number. Each change should come with examples of input that must pass and input that must fail.
As a final exercise, write down five inputs for your own use case and predict each result before running the function. Include a normal value, a boundary value, a blank string, a malformed value, and a very large value. When the results match your written rule, you have a validator whose behavior you can explain—not just a conversion that happens to work for one example.



