7

I want to know how I can get a random text variable in jQuery like this format:

gwtq3tw3232dsk@domain.com

15 digit random combination of letters and numbers in the first part and '@domain.com' in the second part which remains the same.

I want to get real random entries that are different all the time.

how to do this with javascript or jquery?

Thanks

Claudio Delgado
  • 2,239
  • 7
  • 20
  • 27

5 Answers5

10

Use chancejs github

email

chance.email()
chance.email({domain: "example.com"}) 

Return a random email with a random domain.

chance.email()
=> 'kawip@piklojzob.gov'

Optionally specify a domain and the email will be random but the domain will not.

chance.email({domain: 'example.com')
=> 'giigjom@example.com'


Or pure JavaScript

fiddle DEMO

function makeEmail() {
    var strValues = "abcdefg12345";
    var strEmail = "";
    var strTmp;
    for (var i = 0; i < 10; i++) {
        strTmp = strValues.charAt(Math.round(strValues.length * Math.random()));
        strEmail = strEmail + strTmp;
    }
    strTmp = "";
    strEmail = strEmail + "@";
    for (var j = 0; j < 8; j++) {
        strTmp = strValues.charAt(Math.round(strValues.length * Math.random()));
        strEmail = strEmail + strTmp;
    }
    strEmail = strEmail + ".com"
    return strEmail;
}
console.log(makeEmail());
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
8
var chars = 'abcdefghijklmnopqrstuvwxyz1234567890';
var string = '';
for(var ii=0; ii<15; ii++){
    string += chars[Math.floor(Math.random() * chars.length)];
}
alert(string + '@domain.com');

This will randomly pick characters to add to the email string.

Note that this might, once in a blue moon, generate duplicates. In order to completely eliminate duplicates, you would have to store all generated strings and check to make sure that the one you are generating is unique.

JSFiddle Demo.

Robbie Wxyz
  • 7,671
  • 2
  • 32
  • 47
  • I'm trying to use this as the value for a text field. however the script fails when I add these codes and try to execute it. how should I add it, is this right?: ('#field').val(string + '@domain.com'); – Claudio Delgado Nov 16 '13 at 01:28
  • 1
    My edits changed some stuff. Check out the [JSFiddle demo](http://jsfiddle.net/superscript18/7LMjb/) I added, it should work for you. – Robbie Wxyz Nov 16 '13 at 01:32
  • Yes, it does. Perfect! Thanks so much! – Claudio Delgado Nov 16 '13 at 01:34
4

Using the answers from generate a string of 5 random characters

function getRandomEmail(domain,length)
{
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for( var i=0; i < length; i++ )
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text + domain;
}

var email = getRandomEmail("@domain.com",15);
Community
  • 1
  • 1
stevebot
  • 23,275
  • 29
  • 119
  • 181
2

Lets do the trick with toSting to generate alphanumeric string

return Math.random().toString(36).substring(2,11) + '@domain.com';

shortest as possible

If you like to have first character a letter, it could be combination with selection of the first character from the character list

var chars = 'abcdefghijklmnopqrstuvwxyz';
return chars[Math.floor(Math.random()*26)] + Math.random().toString(36).substring(2,11) + '@domain.com';
Dee
  • 282
  • 1
  • 9
0

I think this is better, 1 line of code.

Math.random().toString(36).substring(2)+'@'+(Math.random() * 0xffff).toString(36);