2

I have the following issue.

I have an aspx file upload input that must only show the file dialog if certain text field has a value, otherwise I must simply show an alter saying to fill the field.

I cannot use jquery.

4
  • jquery is just javascript. it just makes things easier, but doesn't do anything you couldn't do yourself with barebones what-you-get-with-the-browser javascript. Commented Aug 10, 2012 at 16:41
  • 1
    I know, but the buzz says I cannot use jquery because the client wants to live in the stone age. Commented Aug 10, 2012 at 16:51
  • What have you tried? I can tell you right now that if the file selector is disabled the dialog wont appear. Commented Aug 10, 2012 at 16:51
  • Simply to disable the upload input, but they specifically want it to not be disabled as to not confuse the user. Commented Aug 10, 2012 at 16:53

1 Answer 1

2

The event object has a preventDefault function that you can use to stop the default from continuing. Using this you can attach a click event to your file upload which will fire upon trying to select a file. From there you can check the value of your text input and return/stop the default of the file element.

(function() {
  var __file = document.getElementById('file');
  var __text = document.getElementById('required');

  __file.addEventListener('click', function (e) {
    e = e || window.event;

    if ( __text.value.length === 0 )
    {
      e.preventDefault();
      return alert('Please fill out the textbox!');
    }


  })
})()

Note: Only tested this in Chrome.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I will test it out and report later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.