NumberInput component only needs the label property to be displayed. Other props can be used to complement its behavior: placeholder, secondaryLabel, onChange... or the number specific ones.
<NumberInput label="Number input component" placeholder="Write what you want here" secondaryLabel="Free text input" />
NumberInput allows setting up the max and min values. However, this is not related to a validation feature but to the component behavior. When setting any of these two properties, it will not be possible to use the component arrows to set a value that is lower than the min prop or a value that is higher than the max prop.
If specific validation is needed, it will need to be handled by the developer. For some details see the Text input validation on change section.
<NumberInput label="Number input component with min and max" placeholder="Enter a value" secondaryLabel="Test the limits with using the arrows" min={20} max={40} />
decimalPlaces allows to specify how many decimal values must be displayed. By default, no restriction is applied and any value entered by the user will be displayed. However, if this property is set (only accepts values from 0 to 100), the value displayed will be modified.
If specific validation is needed, it will need to be handled by the developer. For some details see the Text input validation on change section.
<NumberInput label="Number input component limited decimals" placeholder="Enter a value" secondaryLabel="Try to add an additional decimal" initialValue={5.67} decimalPlaces={2} /> />
NumberInput does not provide a specific validation logic but it provides the way to include the developer validation and use the stateMessages property to display any required error message.
This is an example of handling validation when onChange event is triggered.
exportfunctionNumberInputValidation(){ const[validationMessages, setValidationMessages]=useState({}); const max =100; const min =0; const onChange =useCallback((e, newValue)=>{ setValidationMessages({}); if(newValue &&(newValue > max || newValue < min)){ setValidationMessages({ error:['Value must be between 0 and 100'], }); } },[]); return( <NumberInput label="Enter value" secondaryLabel="Min = 0. Max = 100" stateMessages={validationMessages} min={min} max={max} onChange={onChange} /> ); }
note
There are deprecated versions of this component. Switch to a version of the docs older than 10.0.x to view their docs.
The number input enables users to enter or select numeric values within a predetermined range. It includes controls to incrementally increase or decrease the value. Users can also type numeric values directly into the input field.
Helper text (optional): Provides extra context, hints, or helpful information to aid the user. Often used to explain specific requirements for correctly filling out a field.
Numeric value: changes when the user enters a value into the field or uses the two arrows at the end of the input to add and subtract values.
Number input field: the container, consisting of a fill and a stroke, in which the user enters data.
Use help text to provide guidance about what to input and how. Here are some examples of what you might include in help text:
Context to aid the user, such as how the information will be used
Hints for what kind of information goes inside the input field
Formatting examples or requirements
Only use help text for pertinent information. Avoid using help text that simply restates the same information that appears in the label.
Use sentence case for help text. Write the help text as 1-2 short, complete sentences that end with a period. When showing formatting examples, you don't need to end with a period.
Do use help text to provide additional aid or context to the user.
Don't use help text to simply restate the same information that appears in the label.
Don't put placeholder text in the number entry field. Placeholder text strains users' short-term memory because it disappears once a value is entered. It also poses additional burdens for users with visual and cognitive impairments.
Instead, place hints and instructions, including formatting examples and requirements, outside of the field.