Giter Site home page Giter Site logo

validpic.js's Introduction

validPic.js

Javascript image validator based on mimetypes.

Introduction

Tired of not finding any library to validate images on the client side I decided to create one for my own use and anybody else who want to save some time doing it. Yeah, we all know client side validation is just one of the many steps and we should never rely only on it, but hey! It helps, not only us but also users!

This library makes use of the magic numbers to detect different mimetypes, thefore even when the mimetype is mnodifed we can still figure out it it is what it claims to be.

Install using bower or npm

Optionally, you can install fullPage.js with bower or npm if you prefer: Terminal

// With bower
bower install validpic.js

// With npm
npm install validpic.js

Usage

Valid image types at the moment are: png, jpg, gif, bmp.

  • Include the script before the closing </body> tag.
  • Use the class valid-pic in any input you want to validate.
  • Use the the custom events to manage errors in your own way

Example

<div>
    <input type="file" id="add-image" class="valid-pic" />
</div>

###Using javascript

document.getElementById('add-image').addEventListener("error", function(){
    console.log("Invalid image - javascript custom event");

    //do whatever here
}, false);

###Using jQuery

$('.valid-pic').on('error', function(e){
    var data = e.originalEvent.detail;

    console.log(data);
    console.log("Invalid image - jQuery custom event");
    
    $(data.input).css('background', 'red');
    $(data.input).after('<span class="error-message">Invalid image type!</div>');
});

validpic.js's People

Contributors

alvarotrigo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.