Many of you may often need to restrict file types when dealing with file uploads using input type=’file’. For example, when I was working on testing image conversion functionality, I wanted to limit the uploaded files to image types only.
So, how do you handle this? This is where the accept attribute comes into play.
accept attribute
The main purpose of the ‘accept’ attribute is to restrict the types of files that can be selected. It supports various common attributes:
You can use common MIME types such as image/*, video/*, text/*, and so on.
image/* ➡ Supports all image types, including png, jpeg, gif, and more.
Additionally, you can restrict based on specific file extensions, such as .png, .doc, and so on.
⭐ If you need to specify multiple file types, separate them with a comma (as shown in the example below).”
⬆️ Here is an example I’ve written. You can try it yourself by clicking on it. You’ll notice that in the lower right corner, it changes to ‘Custom Files,’ indicating the specified file types.
Notes
While there may be different outcomes in various browsers, most browsers simply provide a default type restriction. In reality, users can still switch to select other types.
For example, in the previous demo, aside from ‘Custom Files,’ users can switch back to ‘All Files’ and select other types.
How to address this
When encountering this issue, you can’t rely solely on HTML attributes. You must use JavaScript or perform validation on the backend. Here’s a simple example of JavaScript validation.
In this example, the validation function checkfiletype is triggered when the value of the input changes, i.e., when a file is selected for upload.
Here, we use split to split the value based on the period (‘.’). Then, we use pop to retrieve the last element from the resulting array, which is the file type we want.
If it doesn’t match, a notification is displayed, and the content is cleared.
Conclusion
The ‘accept‘ attribute is more like a feature to assist in displaying defaults rather than a true validation mechanism.
This attribute allows users to quickly filter out many unwanted files. For example, if the functionality is to upload documents, you can set it as ‘.doc, .docx, .pdf,’ and so on.
However, as mentioned in the article, a validation mechanism is still necessary. Whether it’s using JavaScript validation like the example provided or backend validation, it remains essential.
var uagb_data = {"ajax_url":"https:\/\/zyrastory.com\/wp-admin\/admin-ajax.php","uagb_masonry_ajax_nonce":"628a58e109"};
var uagb_data = {"ajax_url":"https:\/\/zyrastory.com\/wp-admin\/admin-ajax.php","uagb_masonry_ajax_nonce":"628a58e109","uagb_grid_ajax_nonce":"45da882c03"};
document.addEventListener("DOMContentLoaded", function(){
( function( $ ) {
var cols = parseInt( '3' );
var $scope = $( '.uagb-block-f59a9a34' );
let imagePosition = 'top';if( 'top' !== imagePosition ){
// This CSS is for Post BG Image Spacing
let articles = document.querySelectorAll( '.uagb-post__image-position-background .uagb-post__inner-wrap' );
if( articles.length ) {
for( let article of articles ) {
let image = article.getElementsByClassName('uagb-post__image');
if ( image[0] ) {
let articleWidth = article.offsetWidth;
let rowGap = 20;
let imageWidth = 100 - ( rowGap / articleWidth ) * 100;
image[0].style.width = imageWidth + '%';
image[0].style.marginLeft = rowGap / 2 + 'px';
}
}
}
}
// If this is not a Post Carousel, return.
// Else if it is a carousel but has less posts than the number of columns, return after setting visibility.
if ( ! $scope.hasClass('is-carousel') ) {
return;
} else if ( cols >= $scope.children('article.uagb-post__inner-wrap').length ) {
$scope.css( 'visibility', 'visible' );
return;
}
var slider_options = {
'slidesToShow' : cols,
'slidesToScroll' : 1,
'autoplaySpeed' : 2000,
'autoplay' : Boolean( '1' ),
'infinite' : Boolean( '1' ),
'pauseOnHover' : Boolean( '1' ),
'speed' : 500,
'arrows' : Boolean( '1' ),
'dots' : Boolean( '1' ),
'rtl' : Boolean( '' ),
'prevArrow' : '<button type=\"button\" data-role=\"none\" class=\"slick-prev\" aria-label=\"Previous\" tabindex=\"0\" role=\"button\"><svg width=\"20\" height=\"20\" viewBox=\"0 0 256 512\"><path d=\"M31.7 239l136-136c9.4-9.4 24.6-9.4 33.9 0l22.6 22.6c9.4 9.4 9.4 24.6 0 33.9L127.9 256l96.4 96.4c9.4 9.4 9.4 24.6 0 33.9L201.7 409c-9.4 9.4-24.6 9.4-33.9 0l-136-136c-9.5-9.4-9.5-24.6-.1-34z\"></path></svg><\/button>',
'nextArrow' : '<button type=\"button\" data-role=\"none\" class=\"slick-next\" aria-label=\"Next\" tabindex=\"0\" role=\"button\"><svg width=\"20\" height=\"20\" viewBox=\"0 0 256 512\"><path d=\"M224.3 273l-136 136c-9.4 9.4-24.6 9.4-33.9 0l-22.6-22.6c-9.4-9.4-9.4-24.6 0-33.9l96.4-96.4-96.4-96.4c-9.4-9.4-9.4-24.6 0-33.9L54.3 103c9.4-9.4 24.6-9.4 33.9 0l136 136c9.5 9.4 9.5 24.6.1 34z\"></path></svg><\/button>',
'responsive' : [
{
'breakpoint' : 1024,
'settings' : {
'slidesToShow' : 2,
'slidesToScroll' : 1,
}
},
{
'breakpoint' : 767,
'settings' : {
'slidesToShow' : 1,
'slidesToScroll' : 1,
}
}
]
};$scope.imagesLoaded( function() {
$scope.slick( slider_options );
}).always( function() {
$scope.css( 'visibility', 'visible' );
} );var enableEqualHeight = ( '1' );if( enableEqualHeight ){
$scope.imagesLoaded( function() {
UAGBPostCarousel?._setHeight( $scope );
});$scope.on( 'afterChange', function() {
UAGBPostCarousel?._setHeight( $scope );
} );
}} )( jQuery );
});
ai_front = {"insertion_before":"BEFORE","insertion_after":"AFTER","insertion_prepend":"PREPEND CONTENT","insertion_append":"APPEND CONTENT","insertion_replace_content":"REPLACE CONTENT","insertion_replace_element":"REPLACE ELEMENT","visible":"VISIBLE","hidden":"HIDDEN","fallback":"FALLBACK","automatically_placed":"Automatically placed by AdSense Auto ads code","cancel":"Cancel","use":"Use","add":"Add","parent":"Parent","cancel_element_selection":"Cancel element selection","select_parent_element":"Select parent element","css_selector":"CSS selector","use_current_selector":"Use current selector","element":"ELEMENT","path":"PATH","selector":"SELECTOR"};