Skip to main content

Posts

Html5/JavaScript: Client side image preview before uploading on server

Every time user upload an image or a file, we have to validate it and display preview to user before actually it on server. Now it is possible to read file and its properties on client side (before uploading on server) using HTML5 and JavaScript. Let’s assume we have an uploader control on page having id=imguploader . <input type="file" id="imguploader" name="image" accept="image/*"> Here’s a function I‘ve created that validate the image uploded by user & display the image preview. $('#imguploader').bind('change', function() { var file = this.files[0]; //we can retrive the file array. // alert(file.type); if you want to check image type, uncomment this line. // alert(file.size); if you want to check the image size, uncomment this line. var reader = new FileReader(); // file.target.result holds the DataURL which // can be used as a source of the image: //imgpreview is the id of...
Recent posts

JavaScript: Validate email address

Sometime we need to validate email address entered by user on client side, Here i have written a very simple but useful javascript regex to validate email address. function validate(email) { var reg = /^([A-Za-z0-9_\-\.]{2,})+\@([A-Za-z0-9_\-\.]{2,})+\.([A-Za-z]{2,4})$/; if(reg.test(email) == false) return true; else return false; }

JavaScript functions to get browser name & version

Common javascript functions to get browser name & version, I post here primarily for my own benefit as I always forget the syntax each time I need it. Get Browser Name function getBrowser() { var userAgent = navigator.userAgent.toLowerCase(); if (/webkit/.test( userAgent )) { return "safari"; } if (/opera/.test( userAgent )) { return "opera"; } if (/msie/.test( userAgent ) && !/opera/.test( userAgent )) { return "msie"; } if (/mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )) { return "mozilla"; } }; Get Browser Version function getBrowserVersion() { var userAgent = navigator.userAgent.toLowerCase(); return (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1]; }; Get Browser Type function(){ var isIE11 = function(){ var userAgent = navigator.userAgent.toLowerCase(); return (!$.browser.msie) && (userAgent.indexOf("trident") > 0); }; ...

jQuery : Select all checkboxes in Grid View

While working with Grid View control one of the most common scenarios encountered is that select all child checkboxes. Here I would like to share a quick and really easy code snippet that I have written in jQuery. Let’s start with Grid View code below, here key points are 1) Id of <HeaderTemplate> checkbox.            id="chkParent" 2) CssClass of <ItemTemplate> checkbox.       CssClass="chkChild" <asp:GridView ID="gvMemberBuddies" runat="server" AutoGenerateColumns="false" CellPadding="2" CellSpacing="0" Width="100%"> <Columns> <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="50px" > <HeaderTemplate> <input id="chkParent" name="chkParent" type="checkbox"> </HeaderTemplate> <ItemTemplate> <asp:Ch...

Retrieving query string parameter's value using JavaScript

Today, while working on my project I needed to get query string parameter’s value. Here I would like to share a javascript code snippet that returns an object with querystring parameter and respective value. JavaScript function that returns key/value pairs object: function GetQueryStringParameters(){ var qstring = window.location.search.replace('?', '').split('&'); var qObj = {}; for (var i = 0; i < qstring.length; i++) { var parametername = qstring[i].split('=')[0]; var parametervalue = qstring[i].split('=')[1]; qObj[parametername] = parametervalue; } return qObj; } Example: Let’s assume we have following URL: http://localhost/testpage.aspx?param1=value1&param2=value2 Code to get Object of querystring parameter’s value: $(document).ready(function(){ var qparams = GetQueryStringParameters(); // fetch al...

jQuery: Finding Sibling Position

There have been many times we need to find out the position of an element in respect to its siblings. Here’s a function I‘ve created that returns the position (index) of an element in respect to its siblings. function GetElementPosition(element) { return $(element).parent().children().index($(element)); } Let’s assume we have seven <li> tags, we want to know the position of the element once we click on it. <ul id="weeklist"> <li>Sunday</li> <li>Monday</li> <li>Tuesday</li> <li>Wednesday</li> <li>Thursday</li> <li>Friday</li> <li>Saturday</li> </ul> jQuery Code: $(document).ready(function() { $("#weeklist li").bind('click', function(){ alert( "position: " + GetElementPosition(this)); }); }); Try the demo below, Click on list element to get the position of the element Sunday ...