accept method

accept( mimetype )Returns: Boolean

Description: Makes a file upload accept only specified mime-types.

Uses the HTML5 file API to look at the type attribute of one or more selected files and validate that each matches the specified mime-type.

If nothing is specified, only images are allowed (image/*).

You can specify multiple mime-types by separating them with a comma, e.g. “image/x-eps,application/pdf”.

Works with type=”file” inputs.

Note: This method used to look at just the filename, specifically the file extension. That behaviour is now available as the “extension” method inside src/additional/extension.js.

Example:

Required, only audio files allowed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Required, only audio files allowed:</title>
<link rel="stylesheet" href="http://jqueryvalidation.org/files/demo/site-demos.css">
</head>
<body>
<form id="myform">
<label for="field">Required, audio files only: </label>
<input type="file" class="left" id="field" name="field">
<br/>
<input type="submit" value="Validate!">
</form>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/jquery.validate.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/additional-methods.min.js"></script>
<script>
// just for the demos, avoids form submit
jQuery.validator.setDefaults({
debug: true,
success: "valid"
});
$( "#myform" ).validate({
rules: {
field: {
required: true,
accept: "audio/*"
}
}
});
</script>
</body>
</html>

Demo: