How To Add Custom Jquery Validation For Image File Upload In Summernote?
When “How To Add Custom Jquery Validation For Image File Upload In Summernote?” moves through design, development and browser testing, hiring criteria can clarify hand-offs and time spent on revisions while leaving code quality, accessibility and released behaviour as the real measures of success.
For API changes, browser support and current usage patterns, compare the snippet with the official jQuery website before adopting it in production.
Mar 16, 2021
Hello Friends,
Now let's see example of how to add custom jquery validation for image file upload in summernote. Summernote WYSIWYG editor is a very simple and super fast bootstrap editor. Summernote WYSIWYG editor is a javascript library and it's provide to use their tools for file upload, text bold, color, italy font etc.
We can simply use summernote by following their website "summernote.org". They provide docs for complete guide and we can use in a minute. They also provide file uploading options, But not more for validation like image max size should be 1mb, file mime type should be png, jpg etc. So we can't do it that simple.
However, In this post i will show you how to add custom jquery validation for image upload. we will restrict image size while uploading the image, we will allow to upload only png, jpg and jpeg image file only. So you can do it simply by following jquery code:
JQuery Code
$(document).ready(function() {
$('#summernote').summernote({
height: 300,
callbacks: {
onImageUpload: function(image) {
var sizeKB = image[0]['size'] / 1000;
var tmp_pr = 0;
if(sizeKB > 200){
tmp_pr = 1;
alert("pls, select less then 200kb image.");
}
if(image[0]['type'] != 'image/jpeg' && image[0]['type'] != 'image/png'){
tmp_pr = 1;
alert("pls, select png or jpg image.");
}
if(tmp_pr == 0){
var file = image[0];
var reader = new FileReader();
reader.onloadend = function() {
var image = $('<img>').attr('src', reader.result);
$('#editor').summernote("insertNode", image[0]);
}
reader.readAsDataURL(file);
}
}
}
});
});
So, above simple jquery code, you can understand how simple we write custom jquery validation and you can add your code. I also provide full example for this example so simply take bellow index.html file and see in your local:
Example :
<!DOCTYPE html>
<html>
<head>
<title>Summernote image size limit validation</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />
<script type="text/javascript" src="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css" />
<!-- include summernote css/js-->
<link href="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.4/summernote.css" rel="stylesheet">
<script src="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.4/summernote.js"></script>
</head>
<body>
<div class="container">
<h2>Summernote</h2>
<div id="summernote">Hello Summernote</div>
</div>
</body>
<script type="text/javascript">
$(document).ready(function() {
$('#summernote').summernote({
height: 300,
callbacks: {
onImageUpload: function(image) {
var sizeKB = image[0]['size'] / 1000;
var tmp_pr = 0;
if(sizeKB > 200){
tmp_pr = 1;
alert("pls, select less then 200kb image.");
}
if(image[0]['type'] != 'image/jpeg' && image[0]['type'] != 'image/png'){
tmp_pr = 1;
alert("pls, select png or jpg image.");
}
if(tmp_pr == 0){
var file = image[0];
var reader = new FileReader();
reader.onloadend = function() {
var image = $('<img>').attr('src', reader.result);
$('#editor').summernote("insertNode", image[0]);
}
reader.readAsDataURL(file);
}
}
}
});
});
</script>
</html>
It will help you....
- How to Show Loading Spinner in JQuery?
- How to Create Ajax Submit Form Using jQuery?
- How To Get Current Page URL, Path, Host and hash using jQuery?
- How To Get, Set and Delete Div Background Image jQuery?
- JQuery Remove All Unwanted Whitespace From String Example
- Body Background Video Using Vide Jquery Example
- JQuery UI Autocomplete Example
- JQuery Split String By Comma Into Array Example