Document and PDF (.doc, .docx and .pdf) file validation using Bootstrap and jQuery
Get here reusable file client side validation using Bootstrap and jQuery.
Get here reusable file client side validation using Bootstrap and jQuery.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="form-group"> | |
<label>Upload CV <span class="required">*</span></label> | |
<label class="btn btn-info btn-file"> | |
Browse..<input type="file" id="upload_cv" name="upload_cv" accept=".doc,.docx,.pdf" onchange="validate_file();"> | |
</label> | |
<span class="text-warning">Upload only .doc, .docx and .pdf file.</span> | |
</div> | |
<script type="text/javascript"> | |
//form validation | |
function validate(){ | |
return validate_file(); | |
} | |
function validate_file(){ | |
var file_err = 'file_err'; | |
var upload_cv = $('#upload_cv'); | |
var file = $('#upload_cv')[0].files[0] | |
//hide previous error | |
$("#"+file_err).html(""); | |
if(file == undefined){ | |
upload_cv.parent().after('<span id='+file_err+'><p class="text-danger"><i class="fa fa-times" aria-hidden="true"></i> Please upload CV(.doc, .docx, .pdf) File</p></span>'); | |
return false; | |
}else{ | |
$("#"+file_err).html(""); | |
} | |
console.log(file.size); | |
var fileType = file.type; // holds the file types | |
var match = ["application/pdf","application/vnd.openxmlformats-officedocument.wordprocessingml.document"]; // defined the file types | |
var fileSize = file.size; // holds the file size | |
var maxSize = 2*1024*1024; // defined the file max size | |
// Checking the Valid Image file types | |
if(!((fileType==match[0]) || (fileType==match[1]))) | |
{ | |
upload_cv.val(""); | |
upload_cv.parent().after('<span id='+file_err+'><p class="text-danger"><i class="fa fa-times" aria-hidden="true"></i> Please select a valid (.doc, .docx, .pdf) file.</p></span>'); | |
return false; | |
}else{ | |
$("#"+file_err).html(""); | |
} | |
// Checking the defined image size | |
if(fileSize > maxSize) | |
{ | |
upload_cv.val(""); | |
upload_cv.parent().after('<span id='+file_err+'><p class="text-danger"><i class="fa fa-times" aria-hidden="true"></i> Please select a file less than 2mb of size.</p></span>'); | |
return false; | |
}else{ | |
$("#"+file_err).html(""); | |
} | |
} | |
</script> |
Comments
Post a Comment