How to trigger a message about the invalid form field?

  • 0
    There is a text input with a required attribute that is outside of the form. Is it realistic to trigger a browser error message on it when a button is pressed? Googled the .checkValidity () method, but it doesn't work if the field has no form.
    The overall task is to visually highlight invalid fields for the user after clicking the button. There will be no sending to the server, so the question arose whether it is possible to do without the form in my case.
    JavaScript Anonymous, Sep 20, 2020

  • 1 Answers
  • 0
    Simply. Yuzai. Form. It doesn't matter if it is sent to the server, it won't. It does not matter. If there are text fields, always use the form . So you save yourself and the user a ton of headaches (like the one described in the question).



    A form is an interactive element (like a button, link, etc.). The user can interact with it in many ways, not only by pressing the bear into a button not far away. For example, everyone is familiar with the behavior, especially when there is only one text field: enter something and press Enter. Without a form, this will not work and you will have to be additionally confused for keyboard tracking and so on.
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!