Tag Archives: Web Developer

Find duplicates values in a set of textboxes using jQuery

Well sometime back, I faced this unique problem. It took me sometime to solve it, so I thought of documenting it as my learning so that it is useful for others and might help me one when I need to do it again.

Problem

Okay, the following is the requirement. You have a web page with a set of text boxes. The number of text-boxes might vary based on some backend logic. You need to make sure the user doesn’t enter duplicate values in these set of textboxes. He can leave them blank, but cannot enter duplicate values.

Solution

The following is my solution.

function findDuplicates() {
    var isDuplicate = false;
    jQuery("input[name^='access_keys']").each(function (i,el1) {
        var current_val = jQuery(el1).val();
        if (current_val != "") {
            jQuery("input[name^='access_keys']").each(function (i,el2) {
                if (jQuery(el2).val() == current_val && jQuery(el1).attr("name") != jQuery(el2).attr("name")) {
                    isDuplicate = true;
                    jQuery(el2).css("background-color", "yellow");
                    jQuery(el1).css("background-color", "yellow");
                    return;
                }
            });
        }
    });

    if (isDuplicate) {
        alert ("Duplicate values found.");
        return false;
    } else {
        return true;
    }
}

I basically loop through all the text boxes and compare the values with all the other textboxes, in nested loops. Basically it is like bubble sort. I know this is bad, but was not able to think of any efficient way of doing it. Do you guys can think of a better way?

Posted in JavaScript/jQuery | Tagged , , | 4 Comments

Everyday learning’s as a web developer

One of the good (and also bad) things in being a web developer is that you have to constantly keep (un)learning. Every time I learn a new trick (like changing the default editor in Ubuntu or searching Google efficiently) or get struck due to some bug or wired behavior, I used to document it here, so that I can refer to it later and also for that fact that it might be helpful for others who are searching for the same problem in Google.

Recently I found one such wired behavior in WordPress while retrieving the categories of a post. I wrote about it and almost forgot about it till I received a comment from a person called kaigou

Insert OMFG or variant thereof, about twenty times, at two in the morning. I’ve been fighting with WP all day to get it to do what I want, and the WP docs have been useless — maybe it’s just that most folks don’t really get into really doing wacky things like nested loops and whatnot, I suppose. I was about to give up when behold, the power of google dropped your post on my screen and NOW IT WORKS. I feel like a keyboard mash is due, or buying you a virtual drink, or SOMETHING. May have to settle for quietly dancing around the house in glee, given that it is two in the morning when most non-geeky souls are quietly abed. But not me! Must celebrate your awesomeness for helping me finally getting it to work! Thank you!!!

It was really a very nice feeling to know that you can make someone from the other side of the globe dance at 2 in the morning. 🙂

This comment has given me the much needed motivation to document the day-to-day learning’s of being a web developer. So guys, from now on, if I spend more than 5 minutes on fixing something I will document it here. I also strongly urge you guys to do the same thing in your blogs and if you don’t have a blog then you are always welcomed as a guest writer here 😉

Posted in Random/Personal, Web Programming | Tagged , , | 1 Comment