javascript - Jquery file validator : How to call a function on success? -



javascript - Jquery file validator : How to call a function on success? -

i using jquery file validator plugin. here link : http://adamsanderson.github.io/jquery-file-validator/#documentation can phone call function before validation , oninvalid input. can tell me how can phone call function if file valid input? here code..

$( el ).filevalidator({ onvalidation: function(files){ /* called 1 time before validating files */ }, oninvalid: function(validationtype, file){ /* called 1 time each invalid file */ }, maxsize: '2m', //optional type: 'image' //optional });

ok, few changes need done plugin, not go details theses changes spread in entire file, here version need:

/** uploading files, find large, or wrong type frustrating. `filevalidator` plugin lets warn users before start uploading enormous files. usage ----- select file inputs wish validate, , pass in callbacks handle invalid files. $( el ).filevalidator({ onvalidation: function(files){ ... }, oninvalid: function(validationtype, file){ ... }, maxsize: '2m', //optional type: 'image' //optional }); */ (function($){ validfile = true; $.filevalidator = function(options){ var validations = []; var oninvalid = options.oninvalid; var onvalid = options.onvalid; (var key in $.filevalidator.validations){ if (!options[key]){ continue; } validations.push( $.filevalidator.validations[key](options[key], oninvalid, onvalid)); } homecoming function(file){ for(var i=0, len = validations.length; < len; i++){ validations[i].call(this, file); } if(validfile){onvalid.call();} }; }; $.filevalidator.validations = { maxsize: function(maxsize, invalid, valid){ if( typeof maxsize == 'string' ){ maxsize = $.filevalidator.sizetobytes(maxsize); } homecoming function(file){ if (file.size > maxsize){ invalid.call(this,'maxsize',file); validfile = false; } }; }, type: function(contenttype, invalid, valid){ var isvalid; if( typeof contenttype == 'function' ){ isvalid = contenttype; } else if (contenttype.constructor === regexp ) { isvalid = function(type){ homecoming type.match(contenttype); }; } else { isvalid = function(type){ homecoming ~type.indexof(contenttype); }; } homecoming function(file){ if (!isvalid(file.type)) { invalid.call(this,'type', file); validfile = false; } }; } }; $.fn.filevalidator = function(useroptions) { var options = $.extend({ // validations maxsize: null, type: null, // callbacks onvalidation: $.filevalidator.donothing, oninvalid: $.filevalidator.donothing, onvalid: $.filevalidator.donothing }, useroptions); homecoming this.each(function() { var el = $(this); var validator = $.filevalidator( $.extend({}, options, el.data()) ); el.bind('change', function(event){ var files = this.files || []; options.onvalidation.call(this, files); for(var i=0, len=files.length; < len; i++){ validator.call(this, files[i]); } }); }); }; $.filevalidator.donothing = function donothing(){}; $.filevalidator.sizetobytes = function sizetobytes(size){ var scale = 1; if (~ size.indexof('k')){ scale = 1024; } else if (~ size.indexof('m')){ scale = 1024 * 1024; } else if (~ size.indexof('g')){ scale = 1024 * 1024 * 1024; } homecoming parseint(size,10) * scale; }; })( jquery );

use this:

$(function(){ $('input').filevalidator({ onvalidation : function(){ validfile=true; }, oninvalid: function(type, file){ alert('error: '+type); }, onvalid : function(){ alert('yay!'); }, type: 'image', maxsize: '1m' }); });

js fiddle demo

edit

the validfile variable true beginning, , if meets error, becomes false.

javascript jquery html html5 validation

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -