Skip to content

[Input] add option to validate user input on input with type number #10479

Closed

Description

@nnaydenow

Discussed in #10447

Originally posted by Xidai December 30, 2024
Consider this scenario, there is a Input with type="Number", I only type '-' into the Input and focus out, the '-' is not a valid number, so the event.target.value returns empty, but what we need is to highlight it with error state to tell the user this is not a valid number so that he/she should modify it. But I can not get the real input '-'. Is there any way I can achieve that?

Hello colleagues,

Native input has valueAsNumber property which validates the value, but it returns NaN whether the input is empty, but it still might be valid if required is not set.

In native input it could also be checked with :valid / :invalid selectors which are controlled by element internals.

Activity

  1. changed the title [-][Input] How can I get the value if the input value is not a valid number for type="Number" Input?[/-] [+][Input] add option to validate user input on input with type number[/+] on Jan 7, 2025
  2. added theissue type on Feb 27, 2026
  3. moved this from New to Planned in Planning - Topic RLon Mar 18, 2026
  4. moved this from Planned to New in Planning - Topic RLon Mar 18, 2026
  5. moved this from New to Planned in Planning - Topic RLon Mar 23, 2026
  6. moved this from Planned to In Progress in Planning - Topic RLon Apr 6, 2026
  7. nikoletavnv commented on Apr 23, 2026

    @nikoletavnv
    Contributor

    Hi @nnaydenow ,
    We have discussed the problem and possible solution is for applications to listen to both change and focusout events of the numeric input. When a user types '-' in an empty input and leaves the input , change event will not be fired because the value is not modified - it is empty string regardless the typed '-' or '+' sign. But the users can listen to focusout event and check the validity state of the native input which will be false in that case:

    document.getElementById("inputId").addEventListener("focusout", (e) =>  {
       const invalidNumber = e.target.shadowRoot.querySelector("input").validity.badInput;
       // set the value state of the input depending on the invalidNumber value;
       ...
    });
    

    The focusout event should also be used in case the user enters '-' and then deletes it and leaves the input empty, because in that case a change event will also not be fired.

    For CSS purposes the users can still use the CSS Part we are exposing, but they can only use it for styling the input not showing a value state message:
    [ui5-input]::part(input):invalid{background-color: red;}

    Kind regards,
    Nikoleta

  8. moved this from In Progress to Completed in Planning - Topic RLon Apr 23, 2026
  9. github-actions commented on Apr 23, 2026

    @github-actions

    This issue has been closed. To reopen, just leave a comment!

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions