Home  >  Q&A  >  body text

How to handle "input is not focusable" in javascript form validation?

<p>I have this control</p> <p><br /></p> <pre class="brush:html;toolbar:false;"><div class="conico"> <input class="form-control" readonly id="fileName"/> <input class="form-control" type="file" style="display:none;" required="xxx" id="fileSelect"/> <label for="xxx" class="ico"> <img src="/images/icocamera.svg"> </label> </div></pre> <p><br /></p> <p>I want to fill the textbox with the file name. In the above code, each file has an icon. When the icon is pressed, the corresponding filename should be populated into the input. <br /><br />This works fine, but when the user doesn't select a file and then submit they get an error:</p><p><br />< ;/p> <blockquote> <p>input is not focusable</p> </blockquote> <p>How can I handle this error using a custom javascript function?</p>
P粉208469050P粉208469050445 days ago441

reply all(1)I'll reply

  • P粉897881626

    P粉8978816262023-08-02 16:36:57

    fileName needs required = true, not a file selector.

    Then you can do this:


    $( "form" ).on( "submit", function( event ) {
      if ( $( "#fileName" ).val() ) {
        return;
      }
     
      console.log('not valid')
      event.preventDefault();
    } );

    reply
    0
  • Cancelreply