Skip to content

fix: keep the string value of valid numbers in number inputs - #1339

Merged
snowystinger merged 2 commits into
testing-library:mainfrom
thelastbackspace:fix/1053-number-input-value
Nov 10, 2026
Merged

snowystinger merged 2 commits into
testing-library:mainfrom
thelastbackspace:fix/1053-number-input-value

Conversation

@thelastbackspace

Copy link
Copy Markdown
Contributor

What

Pasting or typing .80 into <input type="number"> gives 0.8 as the value. A browser keeps .80.

Why

sanitizeValue() has a workaround for jsdom, which drops values like 1.. It ran every numeric string through String(Number(v)), so valid values like .80, 1.50 or 007 got rewritten too.

How

I only apply the workaround when the value isn't already a valid floating-point number as the HTML spec defines it. Values like 1. still go through Number() like before.

Added a test in tests/document for setting the value directly, and a paste test that matches the repro in the issue. Both fail without the change. The full jest suite, typecheck and lint pass.

Checklist

  • Documentation (N/A)
  • Tests
  • Ready to be merged

Closes #1053

thelastbackspace and others added 2 commits November 3, 2026 12:28
The JSDOM workaround in sanitizeValue ran every numeric string through
String(Number(v)). That turned values like ".80" into "0.8" and "007"
into "7", so pasting or typing them into <input type="number"> gave a
different value than a browser does.

Only normalize values that are not already a valid floating-point number,
which is what the workaround is for (e.g. "1.").

Closes testing-library#1053

// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-floating-point-number
function isValidFloatingPointNumber(v: string) {
return /^-?(?:\d+|\d*\.\d+)(?:[eE][-+]?\d+)?$/.test(v)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@snowystinger snowystinger left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks good, thanks!

@snowystinger
snowystinger merged commit edef34d into testing-library:main Nov 10, 2026
2 checks passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 14.6.9 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

userEvent.paste possibly casts string to a number

2 participants