Putting type="number" on a measurement field does not finish its validation. I ran into the distinction while reading the input flow for an inches-to-centimeters page: the browser's editing rules, Vue's model conversion, the arithmetic helper, and blur-time validation each handle a different part of the problem.
The conversion factor is not the difficult part. One inch is 2.54 centimeters. The interesting question is what the application should do while a user is clearing a field, entering a negative value, or trying to paste a fractional measurement such as “5 1/2.”
HTML Describes the Control, Not the Whole Domain
The fixed-pair component uses this input on its left side:
<input
type="number"
v-model.number="fromValue"
class="text-input"
inputmode="decimal"
min="0"
step="any"
@blur="validateInput('from')"
/>
Each attribute has a purpose. The number type gives the browser a numeric control. inputmode="decimal" is a keyboard hint for devices with a virtual keyboard. step="any" permits fractional values rather than expressing an integer-only stepping rule.
The minimum of zero describes a validity constraint. It does not mean every attempt to type a negative value is intercepted before JavaScript can observe it, and it does not automatically invoke an application-specific error message.
Vue's .number modifier helps convert model values to numbers, but editing still has transitional states. In particular, clearing a number field can leave an empty string in the model. Treating every model value as an already validated measurement would lose that distinction.
I find it helpful to review numeric forms as a sequence: what can be edited, what reaches the model, what the calculation accepts, and when the UI reports an error. Combining those into a vague statement that “the field only accepts numbers” makes edge cases harder to reason about.
The Shared Conversion Helper Has Its Own Coercion Policy
This page fixes the source unit to inches and the destination to centimeters, but it uses the shared meter-based length conversion helper:
export function convertLength(value, fromKey, toKey) {
const meters = (Number(value) || 0) * (lengthUnitMap[fromKey] || 0);
return lengthUnitMap[toKey] ? meters / lengthUnitMap[toKey] : 0;
}
The inch scale is 0.0254 meters and the centimeter scale is 0.01 meters. Converting 21.5 inches therefore produces 54.61 centimeters after the shared rounding step.
The coercion deserves as much attention as the scales. Number("") is zero, so clearing the source field makes the opposite field calculate a zero result. A nonnumeric string passed directly to this helper also falls back to zero through || 0.
That is a real implementation choice, not a general validation technique. It keeps the result numeric, but collapses “no measurement supplied,” “invalid supplied value,” and “genuine zero” into the same calculation output.
The rounding helper separately returns zero for nonfinite results. If you are building a stricter data-entry application, a numeric zero should not stand in for every failure mode. You would need an explicit validity state, rather than assuming this conversion helper already provides one.
On this page, both fields remain editable. Synchronous watchers and a synchronization guard prevent a derived right-side value from immediately replacing the left-side input. That also allows the source control to remain visibly empty while its counterpart shows the helper's zero result.
Blur-Time Validation Is an Explicit Second Step
The component adds its domain check when a field loses focus:
function validateInput(side) {
const value = side === "from" ? fromValue.value : toValue.value;
if (Number.isFinite(Number(value)) && Number(value) >= 0) return;
message.error(t(`${categoryConfig.value.i18nKey}.invalidInput`));
if (side === "from") fromValue.value = 0;
else toValue.value = 0;
}
For a negative or nonfinite model value, this shows a translated error and resets the relevant field to zero. Because conversion watchers run as values change, a negative intermediate result can exist before blur performs that correction.
That timing is important. The behavior is not “invalid input can never appear.” It is “this check runs when editing leaves the control.” If a future feature submits or exports values without first triggering blur, it should not assume the blur handler already ran.
An empty field is also accepted by this particular check because numeric conversion turns an empty string into zero. That may be acceptable for a casual calculator, but it means the handler does not distinguish an intentionally entered zero from a missing measurement.
The handler reads the model, not the control's validity.badInput state. Browsers can expose an unusable number entry as an empty value, so the translated error is not a guarantee that every malformed edit or rejected paste will be reported. Native validity and application feedback are separate concerns here. If I needed an explicit “please enter a measurement” state, I would check for an empty value before numeric coercion and decide how to handle the control's native validity too; that is not what this implementation currently does.
I would test the model transitions as well as the final result: enter a normal decimal, clear it, enter a negative number, and leave the field. For a two-way converter, repeat those actions on both sides. A happy-path test of 1 inch becoming 2.54 centimeters would not exercise the validation policy at all.
Fractional Notation and Rounding Need Separate Decisions
A decimal number input is not a parser for workshop or height notation. The page has no grammar for 1/2, 5 1/2, or feet-and-inches strings.
If a measurement is five and a half inches, the supported representation is 5.5. Adding support for mixed fractions would require deciding how spaces, slashes, zero denominators, signs, and unit marks should behave. It is not something supplied by the existing conversion factor.
There is a precision limit too. The shared rounding helper is:
export function roundLengthResult(v) {
if (!isFinite(v)) return 0;
return Math.round(v * 1e6) / 1e6;
}
For ordinary measurement magnitudes, this rounds the derived result to at most six decimal places. It is not just text formatting: the watchers assign that rounded number to the opposite field's model. Significant figures and manufacturing tolerances are not preserved.
For example, entering 1 centimeter on the right produces 0.393701 inches on the left. Converting that rounded inch value back gives 1.000001 centimeters, not exactly 1. The synchronization guard prevents that reverse calculation from immediately overwriting the centimeter value the user just entered. I would keep unrounded calculation state separate from formatted output if repeated conversions needed to preserve more precision; this page does not do that.
The finite-value check is not an unlimited range guarantee, either. A very large but finite input can overflow during conversion or during multiplication by a million in the rounding helper. The helper maps an already nonfinite input to zero, but it does not recheck the result of that multiplication. This is another reason not to reuse the calculation as a strict validation boundary.
Finally, interpreting a product measurement remains the reader's job. A monitor's 21.5-inch size typically refers to its diagonal; converting that scalar to centimeters does not calculate width or height. Likewise, a converted suitcase dimension is not a guarantee of compliance with a particular airline's baggage rules.
I turned this numeric-input flow into a small free tool: Inches to CM Converter.
Top comments (0)