Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random 'email format' text using jQuery

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

[email protected]

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

like image 886
Claudio Delgado Avatar asked Nov 16 '13 01:11

Claudio Delgado


2 Answers

Use chancejs github

email

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

Return a random email with a random domain.

chance.email()
=> '[email protected]'

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

chance.email({domain: 'example.com')
=> '[email protected]'


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());
like image 128
Tushar Gupta - curioustushar Avatar answered Sep 27 '22 19:09

Tushar Gupta - curioustushar


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.

like image 36
Robbie Wxyz Avatar answered Sep 27 '22 19:09

Robbie Wxyz