Lập trình · 26/09/2026

JavaScript parseInt: A Numeric Result Does Not Validate the Input

A quantity field accepts “12abc” and saves twelve. Treating parseInt as validation can silently accept unintended input. Converting text to a number and verifying the entire input are separate responsibilities.

JavaScript parseInt: Đọc được số chưa có nghĩa đầu vào hợp lệ

A quantity field accepts “12abc” and saves twelve. Treating parseInt as validation can silently accept unintended input. Converting text to a number and verifying the entire input are separate responsibilities.

1. A parsed prefix does not validate the whole string

The MDN parseInt reference describes parsing by radix. Parsing can stop at an unsupported character and return the numeric prefix.

console.log(parseInt('12abc', 10)); // 12
console.log(parseInt('12.5', 10));  // 12

These are expected results, not JavaScript defects. If the product requires a whole integer string without a suffix, validate its structure rather than treating any numeric result as success.

2. Define an explicit quantity contract

This example accepts ASCII digits, trims surrounding whitespace and permits leading zeros. Values must be between zero and 1000. Signs, decimal points and exponent notation are rejected. Adapt this illustrative policy to your product.

function parseQuantity(raw) {
  if (typeof raw !== 'string') {
    throw new TypeError('Expected text input');
  }
  const text = raw.trim();
  if (!/^[0-9]+$/.test(text)) {
    throw new TypeError('Use decimal digits only');
  }
  const quantity = Number(text);
  if (!Number.isSafeInteger(quantity) || quantity > 1000) {
    throw new RangeError('Quantity must be from 0 to 1000');
  }
  return quantity;
}

Empty input is rejected before Number can turn it into zero. "0012" becomes twelve by the stated policy. If leading zeros matter, as with identifiers, preserve a string instead of using this function.

3. Finite is not always sufficient

Number.isFinite checks an already numeric finite value without coercing strings. However, 12.5 is finite too. Integer requirements and business limits need their own checks.

Do not silently replace invalid quantities with zero or defaults. Report the problem so users can correct it rather than assigning a different meaning.

4. Consider localized formats

The example does not interpret “1.000” or “1,000” as one thousand. If localized grouping or decimal separators are supported, specify their rules and show input examples. Removing punctuation without context can change the value.

Client validation provides quick feedback; the backend must still validate types, ranges and authorization. Requests may come from clients other than your form.

5. Minimum test cases

  • "0", "12", "0012" and " 12 " follow the documented policy.
  • Empty, whitespace-only and "12abc" inputs are rejected.
  • "12.5", "1e3", "-1" and "+1" are rejected by this example.
  • "1000" passes and "1001" exceeds the limit.
  • null, objects and direct numbers are not automatically stringified.
  • Extremely large numeric strings fail safe-integer or range checks.

Choose a parser for the permitted syntax, then validate the parsed value. Receiving a number alone does not establish valid input.

Discussion

Comments 0

Sign in to comment

You need an account to join the discussion and reply to other readers.

Sign inRegister

No comments yet. Be the first to share your thoughts.