Skip to content

:user-invalid & :user-valid browser support #178

Closed

Description

@brandonmcconnell

Description

Quoting the specification:

The :user-invalid and the :user-valid pseudo-classes represent an element with incorrect or correct input, respectively, but only after the user has significantly interacted with it.

The :user-invalid pseudo-class must match an :invalid, :out-of-range, or blank-but-:required elements between the time the user has attempted to submit the form and before the user has interacted again with the form element.

The :user-valid pseudo-class must match a :valid element between the time the user has attempted to submit the form and before the user has interacted again with the form element.

User-agents may allow them to match such elements at other times, as would be appropriate for highlighting an error to the user. For example, a UA may choose to have :user-invalid match an :invalid element once the user has typed some text into it and changed the focus to another element, and to stop matching only after the user has successfully corrected the input.

Rationale

There has been plenty of discussions and workarounds supporting this for over a decade now (or at least as far back as I can remember)— not so much the pseudo itself so much as clunky methods to accomplish what this pseudo would provide effortlessly.

Oftentimes, we want to validate/invalidate form data, but we don't want to show the field as valid or invalid until after the first change event so the user has a chance to interact with the form field before the page applies successful/failure styling to the element.

Specification

https://w3c.github.io/csswg-drafts/selectors-4/#user-pseudos

Tests

Activity

  1. added this to the milestone on Oct 2, 2022
  2. moved this to Proposed in Interop 2023on Oct 7, 2022
  3. removed this from the milestone on Oct 7, 2022
  4. annevk commented on Oct 28, 2022

    @annevk
    Member

    I wonder if whoever implemented these in Firefox can help out with whatwg/html#8452 as it's not clear how they work in terms of HTML. Selectors doesn't dictate the rules for host languages after all.

  5. jgraham commented on Nov 9, 2022

    @jgraham
    Contributor

    Seems like @emilio did the Gecko implementation.

  6. emilio commented on Nov 9, 2022

    @emilio

    I mostly rejiggered :-moz-ui-{valid,invalid}, which were pre-existing. The rules are a bit complex, but I can try to explain them:

    • For <button>, :valid and :user-valid are equivalent (code
    • Seems like the same is true for form-associated custom-elements (code). That was implemented afterwards, maybe @EdgarChen can comment. Might be useful to allow authors to customize them.
    • For <input>, <select> and <textarea> the rules are described in this comment. <select> code is here, <textarea> is here.
  7. annevk commented on Nov 9, 2022

    @annevk
    Member

    I think for this to be part of Interop 2023 someone would have to turn that into a PR against the HTML Standard. A lot of it looks somewhat straightforward, but some of the state keeping and when to invalidate that state might be tricky I suppose. And yeah, I suspect that for form-associated elements we need to reconsider when they would apply.

  8. foolip commented on Nov 11, 2022

    @foolip
    Member

    In the State of CSS 2022 question about browser incompatibilities, there was one mention of :user-invalid and one of :user-valid, so not enough to make the top list in #248.

    (I'm commenting on all proposals where I could find some mention at all in State of CSS results.)

  9. foolip commented on Nov 11, 2022

    @foolip
    Member

    In the MDN short survey on CSS & HTML, "CSS :user-invalid and :user-valid pseudo-classes" was selected by ~11% of survey takers, putting it in the bottom third of the 20 options. (There is some uncertainty as with any survey data.)

  10. mfreed7 commented on Nov 12, 2022

    @mfreed7

    I think for this to be part of Interop 2023 someone would have to turn that into a PR against the HTML Standard. A lot of it looks somewhat straightforward, but some of the state keeping and when to invalidate that state might be tricky I suppose. And yeah, I suspect that for form-associated elements we need to reconsider when they would apply.

    I agree with this comment. It's tough to tell what the standardized behavior is or should be for this. E.g. there seems to be a lot of flexibility built into this sentence:

    User-agents may allow them to match such elements at other times, as would be appropriate for highlighting an error to the user.

    @emilio would it be possible to expand on your comment just a bit to summarize the behavior itself?

  11. added
    accepted
    on Feb 1, 2023
  12. foolip commented on Feb 1, 2023

    @foolip
    Member

    Thank you for proposing :user-invalid and :user-valid for gradients for inclusion in Interop 2023.

    We are pleased to let you know that this proposal was accepted as part of the Custom Properties focus area. You can follow the progress of this Focus Area on the Interop 2023 dashboard.

    For an overview of our process, see the proposal selection summary. Thank you for contributing to Interop 2023!

    Posted on behalf of the Interop team.

  13. brandonmcconnell commented on Feb 18, 2023

    @brandonmcconnell
    Author

    Thank you for proposing :user-invalid and :user-valid for gradients for inclusion in Interop 2023.

    @foolip Just to confirm, this is unrelated to gradients

  14. foolip commented on Feb 18, 2023

    @foolip
    Member

    @brandonmcconnell sorry, of course, that was copypasta.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedfocus-area-proposal

    Type

    No type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions