Skip to main content

Document and PDF (.doc, .docx and .pdf) file type validation using jQuery

Document and PDF (.doc, .docx and .pdf) file validation using Bootstrap and jQuery




Get here reusable file client side validation using Bootstrap and jQuery.




<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

Popular Posts

How to use terminal within the Sublime Text editor?

Sublime Text is primarily a text editor and does not have a built-in terminal like some other integrated development environments (IDEs) do. However, you can use the terminal from within Sublime Text by installing a package called Terminal and Terminus . To use the terminal in Sublime Text using Terminal package, follow these steps: Install Package Control (if you haven't already): Open Sublime Text. Press Ctrl + (backtick) to open the Sublime Text console. Copy and paste the installation code for Package Control from the official website: https://packagecontrol.io/installation Press Enter to execute the code. Wait for Package Control to install. Install the "Terminal" package: Press Ctrl + Shift + P (Windows/Linux) or Cmd + Shift + P (Mac) to open the command palette. Type "Package Control: Install Package" and select it from the command palette. In the package list, type "Terminal" and select the "Terminal" package to install it. Open t...

How to take user input from terminal(stdin) in Rust?

In Rust, you can use the std::io module from the standard library to read input from the user. Here's an example that demonstrates how to get input from the user: use std::io; fn main() { // Create a new instance of `std::io::stdin` for reading user input let mut input = String::new(); // Prompt the user for input println!("Enter your name:"); // Read input from the user io::stdin() .read_line(&mut input) .expect("Failed to read line"); // Trim any trailing whitespace or newlines from the input let name = input.trim(); // Display the user's input println!("Hello, {}!", name); } In this example, we create a mutable String variable named input to store the user's input. We then use the std::io::stdin() function to obtain a handle to the standard input stream. Next, we call the read_line() method on the input stream, passing a mutable reference to the input variable. The r...

Different types of variables in Python with examples.

In Python, instance variables, static variables, and local variables are all different types of variables that serve different purposes within a program. Instance Variables: Instance variables are unique to each instance of a class. They are defined within a class's methods or the __init__ method and are accessed using the self keyword. Each instance of a class maintains its own copy of instance variables. These variables hold data specific to each object and can have different values for each instance of the class. Here's an example that demonstrates instance variables: class Person: def __init__(self, name, age): self.name = name # instance variable self.age = age # instance variable person1 = Person("Alice", 25) person2 = Person("Bob", 30) print(person1.name) # Output: Alice print(person2.name) # Output: Bob print(person1.age) # Output: 25 print(person2.age) # Output: 30  In the example above, name and a...