4

I've got an file-input which is submitted with a submit button. But theres also a delete button in the same form. So my problem is, when i press the delete button it will say "Please select an file" because the "required" attribute is in the file-input. I want that this message only displays when you press the upload button(of course only if the input is empty).

Here's my Code :

<form>
<input name="file" id="file" type="file" required>
<input type="submit" name="sub_image" value="Upload">
<input type="submit" name="del_image" value="Delete">
</form>
3
  • Sorry, but this doesn't make sense: Delete which file - the one you are about to upload??? Commented Jun 8, 2017 at 22:22
  • 1
    Thanks @j08691 Sometimes its just so simple that you didnt even thought about that :D Commented Jun 8, 2017 at 22:24
  • In addition to @j08691 comment, are you trying to reset the file selection? If that's the case then set type="reset" for del_image input. Also, using <button> instead of <input> is even better. Commented Jun 9, 2017 at 0:01

2 Answers 2

5

You could use formnovalidate attribute to skip validation on specific submit button:

<form>
<input name="file" id="file" type="file" required>
<input type="submit" name="sub_image" value="Upload">
<input type="submit" name="del_image" value="Delete" formnovalidate >
</form>
Sign up to request clarification or add additional context in comments.

Comments

1

(function ($) {
	
    $('#my-form').validate({
        rules: {},
        messages: {},
        submitHandler: function () {
            return false
        }
    });
    $('#del_image').click(function(){
	
		$("#file").val("");
	});
    

})(jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.min.js"></script>

<form id="my-form">
<input name="file" id="file" type="file" required>
<input type="submit" name="sub_image" value="Upload">

<button id="del_image">Delete</button>
</form>

Comments

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.