Skip to main content

Posts

Showing posts with the label Javascript

Sorting an array of objects using Javascript

 When working on one of my projects, I had to sort the elements of the JSON array of objects. I did this most of the time from server side but this time I had to do the client side sort. Here is the solution what I found. We've two functions one to sort by ascending and other for descending. It expects first parameter as array (of objects) and second parameter the key on basis of which we want to sort. <script> function sortByKeyDesc(array, key) { return array.sort(function (a, b) { var x = a[key]; var y = b[key]; return ((x > y) ? -1 : ((x y) ? 1 : 0)); }); } </script> Here is the example <script> books = [{ 'BookID': 123, 'BookName': 'Seven Habits of most effective people' }, { 'BookID': 234, 'BookName': 'Why Leaders eat last' }, { 'BookID': 345, 'BookName': 'How to influence people' }, { 'BookID': 456, 'BookName': 'The Tipping...

Multiple Checkboxes Validation

This is a regular problem and solution is hardly available on google. When we use multiple checkboxes of same group (same name with square brackets) and you've to read the values from server side, then below solution will help you. Here I'm using jQuery (https://jquery.com/) and jQuery Validate plugin (https://jqueryvalidation.org/) For an example, I've to ask a user which of the listed book they are interested to know about <form id="BooksForm" method="post" name="BooksForm"> <p>Books you are interested in </p> <input class="Books" id="Book1" name="Books[]" type="checkbox" value="1" /> The Inner Game of Tennis <br /> <input class="Books" id="Book2" name="Books[]" type="checkbox" value="1" /> Monk who sold his ferrari <br /> <input class="Books" id="Book3" name=...

WYSIWYG super light editor with jQuery & Bootstrap

Yes! It is. Summernote is the super light WYSIWYG editor for the web. It supports most of the features tinyMCE or CKEditor does but with smallest Javascript & CSS files. You can download Summernote from here . It is the official website for the same. It has very simple getting started tutorial.  This is article will show you how to start with summernote and couple of fixes or plugin which you don't find on the official website. Getting started: If you want to download and start with, click here . For CDN, place below snippet under head tag. <!-- include libraries(jQuery, bootstrap) --> <link href="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.css" rel="stylesheet"> <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script> <script src="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.js"></script> <!-- include summernote css/js --...

Validation and Submission of form using jQuery :: Easiest Solution

In any application, form validation and submission to server is so common and redundant; there are multiple ways of doing it. And the best and easiest way that I do use or recommend is jQuery solution.  Most popular jQuery plugins are the best solution, they have the best solution ever.  One, jQuery Validator written and maintained by Jorn Zaefferer, a member of the jQuery team, lead developer on the jQuery UI team and maintainer of QUnit. Two, jQuery Form by malsup, it has various plugins and solutions. Another plugin that I like from same developer is jQuery BlockUI . (give a try). Name: Email: Note: To avoid unexpected behavior, maintain same form and fields names and id selector values. Need further explanation, let me know.

Email already exists using jQuery

It is the common requirement for any web application either it is PHP, .net, JSP etc., which has users involved. When creating/adding user from admin or end user registration we should check whether username or email address already exists. It will help to have unique registrations to our application and avoid confusion between various users.  I see many developers struggle with this by submitting the form to server side script and validate their from database, in case username/email address already exists they take the pain to go back to previous page or form and show the error message with other form data filled.  Few use AJAX to send the information onblur or onkeyup or onkeydown but face difficulties to stop form submission. Here is the simple solution that would help in validating username or email address existence without much hassle.  Solution comes from popular jQuery Validator plugin I'm using this from ages.  HTML Code: <html> <head...

JSON array of objects

It can be a situation where you got a array of data that should be sent in the JSON format to server via ajax, then you can do the following <script type="text/javascript" > groups = ['A', 'B', 'C']; users  = ['user1', 'user2', 'user3']; //required format is  [{"group_name" : "A", "leader" : "user1"}, {"group_name" : "B", "leader" : "user2"}, {"group_name" : "C", "leader" : "user3"}] group_leaders = new Array(); len = groups.length; for(var i = 0; i < len; i++ ){     group_leaders.push({"group_name" : groups[i], "leader" : users[i]}); } alert(JSON.stringify(group_leaders)); </script> Here you can test here http://jsfiddle.net/sailesh/6wXdY/