Home   Subscribe   Linkedin
  Archive Contact  

Move ListBox Item to Another ListBox Using Jquery

In one of the modules I have coded ,I need to transfer items from one ListBox(Select) to another.I have put the same code here.

Here listBox1 and listBoxb2 are the two listboxes and btnTransfer and btnTransferBack are the two buttons one whose click we 

will transfer the selected items.I have also put a check on the maximum number of items that can listbox2 can contain(in this case it is 20).

<select id="listBox1" style="width:180px;height:210px" size=10 multiple="multiple"/>
 <
select id="listBox2" style="width:180px;height:210px" size=10 multiple="multiple"/>
 <
button id="btnTransfer" onclick="listbox_moveacross('listBox1','listBox2', 20);return false;"> </button>
 <
button id="btnTransferBack" onclick="listbox_moveacross('listBox2', 'listBox1', -1);return false;"> </button>
<script type="text/javascript">
function listbox_moveacross(sourceID, destID, limit) {
            if (limit > -1) {
                var startIndex = 0;
                var endIndex = 0;
                endIndex = limit - $('#' + destID + ' option').length;
                $('#' + sourceID + ' option:selected').slice(startIndex, endIndex).appendTo('#' + destID);
            }
            else {
                $('#' + sourceID + ' option:selected').appendTo('#' + destID);
            }
        }
</script>
Posted by: Rishabh Toshniwal

Categories: ASP.NET, Jquery

Tags: , ,

C# Program to Auto-generate Passwords

This post will look into a common scenario where we need to auto generate password for a new sign-up and may be send that to the customer. The program below generates a random password between 6-12 chars and has both alphanumeric and non-alphanumeric characters. We can introduce more conditions as we like.

 

using System;
using System.Security.Cryptography;

namespace TestForClient
{
    public class RandomPasswordGenerator
    {
        private static int MAX_LENGTH = 12;
        private static int MIN_LENGTH = 6;

        private static string PASSWORD_ALPHANUMERIC = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
        private static string PASSWORD_NONALPHANUMERIC = "~!*@#$%^&-_=+\'/?.><";

        public static string GeneratePassword()
        {
            char[][] charGroups = new char[][] 
        {
            PASSWORD_ALPHANUMERIC.ToCharArray(),
            //PASSWORD_CHARS_UPPER.ToCharArray(),
            //PASSWORD_CHARS_NUMERIC.ToCharArray(),
            PASSWORD_NONALPHANUMERIC.ToCharArray()
        };

            int[] charsLeftInGroup = new int[charGroups.Length];
            for (int i = 0; i < charsLeftInGroup.Length; i++)
                charsLeftInGroup[i] = charGroups[i].Length;

            int[] leftGroupsOrder = new int[charGroups.Length];
            for (int i = 0; i < leftGroupsOrder.Length; i++)
                leftGroupsOrder[i] = i;

            // Random number generator to use a real seed
            byte[] randomBytes = new byte[4];
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            rng.GetBytes(randomBytes);
            int seed = (randomBytes[0] & 0x7f) << 24 |
                        randomBytes[1] << 16 |
                        randomBytes[2] << 8 |
                        randomBytes[3];

            Random random = new Random(seed);
            char[] password = null;
            password = new char[random.Next(MIN_LENGTH, MAX_LENGTH + 1)];
            
            int nextCharIndex;
            int nextGroupIndex;
            int nextLeftGroupsOrderIndex;
            int lastCharIndex;
            int lastLeftGroupsOrderIndex = leftGroupsOrder.Length - 1;

            for (int i = 0; i < password.Length; i++)
            {
                if (lastLeftGroupsOrderIndex == 0)
                    nextLeftGroupsOrderIndex = 0;
                else
                    nextLeftGroupsOrderIndex = random.Next(0,
                                                         lastLeftGroupsOrderIndex);
                nextGroupIndex = leftGroupsOrder[nextLeftGroupsOrderIndex];
                lastCharIndex = charsLeftInGroup[nextGroupIndex] - 1;
                if (lastCharIndex == 0)
                    nextCharIndex = 0;
                else
                    nextCharIndex = random.Next(0, lastCharIndex + 1);
                password[i] = charGroups[nextGroupIndex][nextCharIndex];

                // If we processed the last character in this group, start over.
                if (lastCharIndex == 0)
                    charsLeftInGroup[nextGroupIndex] =
                                              charGroups[nextGroupIndex].Length;
                else
                {
                    // Swap processed character with the last unprocessed character
                    // so that we don't pick it until we process all characters in
                    // this group.
                    if (lastCharIndex != nextCharIndex)
                    {
                        char temp = charGroups[nextGroupIndex][lastCharIndex];
                        charGroups[nextGroupIndex][lastCharIndex] =
                                    charGroups[nextGroupIndex][nextCharIndex];
                        charGroups[nextGroupIndex][nextCharIndex] = temp;
                    }
                    // Decrement the number of unprocessed characters in
                    // this group.
                    charsLeftInGroup[nextGroupIndex]--;
                }

                // If we processed the last group, start all over.
                if (lastLeftGroupsOrderIndex == 0)
                    lastLeftGroupsOrderIndex = leftGroupsOrder.Length - 1;
                else
                {
                    if (lastLeftGroupsOrderIndex != nextLeftGroupsOrderIndex)
                    {
                        int temp = leftGroupsOrder[lastLeftGroupsOrderIndex];
                        leftGroupsOrder[lastLeftGroupsOrderIndex] =
                                    leftGroupsOrder[nextLeftGroupsOrderIndex];
                        leftGroupsOrder[nextLeftGroupsOrderIndex] = temp;
                    }
                    lastLeftGroupsOrderIndex--;
                }
            }
            return new string(password);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 10; i++)
                Console.WriteLine(RandomPasswordGenerator.GeneratePassword());

            Console.ReadKey();
        }
    }
}


Above code has been tested and works fine. Please try this when you need and let us know in case of any issues.

Thanks.

Posted by: sushant.pandey

Categories: ASP.NET, C#

Tags: , ,