0

code:

function alphanumeric()

{

var randomnum=Math.random().toString(36).substr(2,8);

document.getElementbyId("textBoxId").value = randomnum;

}

Each time when I call alphanumeric() function this will generate alphanumeric random string and show on the Text Box like.. v3ta4t8l,zshqoc2u,9pey71rb...which works fine.. In my requirement I need to allow only unique alphanumeric. Could you please help me to solve my below question,

1.Is it only generate unique alphanumeric?

2.How many alphanumeric this will generate?

3.Is it possible to allow only unique alphanumeric?

4.It is possible to store unique alphanumeric in database use of primary key in my case already one of my column having primary key and so I will try to implement with the use of java script...

vicky
  • 27
  • 2
  • 7
  • Have you ever heard of GUIDs? – Matt Ball May 22 '14 at 05:14
  • sorry..I do not have enough idea about that... – vicky May 22 '14 at 05:29
  • I have looked at the answers and your question and I'm not sure what exactly you mean by unique. Do you want no repeated characters within one string or do you want to emit a series of unique strings when you call `alphanumeric()` repeatedly? – M Oehm May 22 '14 at 05:29
  • I don't need to show any repeated same value...sorry for the inconvenient.. – vicky May 22 '14 at 05:42
  • Related: [how to generate uuid in javascript](http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript) – Ja͢ck May 22 '14 at 05:48

2 Answers2

1

Here's something that would return only unique alphanumerics

function alphanumeric_unique() {
    return Math.random().toString(36).split('').filter( function(value, index, self) { 
        return self.indexOf(value) === index;
    }).join('').substr(2,8);
}

FIDDLE

Splitting the string into an array of characters, then using Array.filter() to filter out any characters that are already in the array to get only one instance of each character, and then finally joining the characters back to a string, and running substr(2, 8) to get the same length string as in the question, where it starts at the second character and gets a total of eight characters.

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Thanks..adeneo... could you please tell me, How many alphanumeric this will generate? – vicky May 22 '14 at 05:40
  • @user3333029 - It will generally exactly as many as the original function does, as it uses substr() after the duplicate alphanumnerics have been weeded out. – adeneo May 22 '14 at 05:43
  • Once again thanks... adeneo... let me know could you please little bit text about this code. I am beginner.Hope that will more helpful to me... – vicky May 22 '14 at 05:55
1
 function IDGenerate() {
        var text = "";
        var hdntxt = "";
        var captchatext = "";
        var possible = "ABCDEFGHIkLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        for (var i = 0; i < 7; i++) {
            text += possible.charAt(Math.floor(Math.random() * possible.length));         
        }

        document.getElementById("txtboxID").value = text;
        return false;
    }
Venki
  • 535
  • 2
  • 8
  • 21
  • Don't you need to `splice` the used character out of `possible` after you've used it? – M Oehm May 22 '14 at 05:37
  • For that We Should use System GUID only.It is very bad idea to check every time to database when it is generated or else we can keep some Formats like Time+Username it also creates uniqueness.Time can get in Javascript and If user logged in session value can be get in Javascript. both can combine and go ahead for Uniqueness. – Venki May 22 '14 at 06:05