电子邮件生成器(Javascript)

时间:2017-01-28 20:00:04

标签: javascript email

我正在尝试创建一个电子邮件生成器(供个人使用),它需要输入first和lastname,然后使用我创建的数组中的域名列表。 从site得到了这个想法,并根据我的需要调整了一下。

以下是示例代码。

function permutate(form) {

  var firstlastname = form.firstlastname.value.toLowerCase().trim();
  var namesplit = firstlastname.split(" ");
  var firstname = namesplit[0];
  var lastname = namesplit[1];
  var domain = ['gmail.com','yahoo.com','hotmail.com','msn.com','outlook.com','live.com','me.com','aol.com','mac.com'];

  if (firstname==null || firstname==""){
    document.getElementById("results").innerHTML= "At least enter a first name to get started!";
    return false;    
  } else if (lastname==null || lastname==""){
    document.getElementById("results").innerHTML= "Come on!, enter a last name!";
    return false;    
  } else {}
  for (var i=0; i<domain.length;i++)
  var p1 = firstname + "@" + domain[i] + ",";
  var p2 = firstname + lastname + "@" + domain[i] + ",";
  var p3 = firstname + "." + lastname + "@" + domain[i] + ",";
  var p4 = firstname.charAt(0) + lastname + "@" + domain[i] + ",";
  var p5 = firstname.charAt(0) + "." + lastname + "@" + domain[i] + ",";
  var p6 = firstname + lastname.charAt(0) + "@" + domain[i] + ",";
  var p7 = firstname + "." + lastname.charAt(0) + "@" + domain[i] + ",";
  var p8 = firstname.charAt(0) + lastname.charAt(0) + "@" + domain[i] + ",";
  var p9 = firstname + "_" + lastname + "@" + domain[i] + ",";
  var p10 = firstname.charAt(0) + "_" + lastname + "@" + domain[i] + ",";
  var p11 = lastname + firstname + "@" + domain[i] + ",";
  var p12 = lastname + "." + firstname + "@" + domain[i] + ",";    
  var p13 = lastname + firstname.charAt(0) + "@" + domain[i] + ",";
  var p14 = lastname + "." + firstname.charAt(0) + "@" + domain[i];

  var addresses= p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 + p10 + p11 + p12 + p13 + p14;

    document.getElementById("results").innerHTML= addresses;
}

结果只会打印数组的最后一个值,它总是“mac.com”所以它将是“vb @ mac.com,vb @ mac.com,vb @ mac.com ......”

我在想它会显示所有可能的组合,比如

vb@gmail.com,v.b@gmail.com,vb@gmail.com...
vb@yahoo.com,v.b@yahoo.com,vb@yahoo.com...
vb@hotmail.com,v.b@hotmail.com,vb@hotmail.com...

等。

我是否正确地做了,或者有什么我需要添加或者可以做到吗?

我刚刚学习编码时一直在敲打头脑,需要专业人士的指导。

我想要做很长的路,但我想我会有很长的代码。

firstname + "@" + "gmail.com" + ",";
firstname + lastname + "@" + "yahoo.com" + ","

编辑:从头开始,将根据以下评论继续采用不同的方法。

1 个答案:

答案 0 :(得分:2)

我尝试了一下,得到result。你可以在这里看到代码:

html:

 error: field ‘mother’ has incomplete type
   struct Cat mother;
              ^

css:

<form name="names">
  <input type="text" id="box1" name="firstlastname" placeholder="Luke Skywalker"><br>
<br>
  <input type="submit" id="button-make" value="Make Variations" onClick="permutate(this.form);return false;">
</form>

<input type="submit" id="button-reset" value="Reset" onClick="reset('results');return false;">

<div id="results"></div>

的javascript:

#form {
  margin-top: 20px;
}

input[type] {
  font-size: 25px;
  font-family: arial;
}

#box1 {
  width: 317px;
  padding: 10px 12px;
  margin-bottom: 10px;
  border: 1px solid rgb(200, 200, 200);
  border-top-color: rgb(100, 100, 100);
  border-radius: 5px;
  color: #5A5A5A;
}

#button-reset {
  position: relative;
  display: inline-block;
}

#button-make,
#button-reset {
  float: left;
}

#button-reset {
  margin: 0 0 0 10px;
}

#results {
  position: relative;
  height: 200px;
  display: block;
  overflow: auto;
  margin-top: 45px;
  padding: 15px;
  border: 1px solid rgb(200, 200, 200);
  /*border-bottom: 1px solid rgb(200,200,200);*/
  background: rgb(245, 245, 245);
  color: rgb(50, 50, 50);
  font-size: 20px;
  line-height: 36px;
  font-family: arial;
}

我更改了function permutate(form) { var firstlastname = form.firstlastname.value.toLowerCase().trim(); var namesplit = firstlastname.split(" "); var firstname = namesplit[0]; var lastname = namesplit[1]; var arr = []; var domain = ['gmail.com', 'yahoo.com', 'hotmail.com', 'msn.com', 'outlook.com', 'live.com', 'me.com', 'aol.com', 'mac.com']; if (firstname == null || firstname == "") { document.getElementById("results").innerHTML = "At least enter a first name to get started!"; return false; } else if (lastname == null || lastname == "") { document.getElementById("results").innerHTML = "Come on!, enter a last name!"; return false; } else { for (var i = 0; i < domain.length; i = i + 1) { var p1 = firstname + "@" + domain[i] + ","; var p2 = firstname + lastname + "@" + domain[i] + ","; var p3 = firstname + "." + lastname + "@" + domain[i] + ","; var p4 = firstname.charAt(0) + lastname + "@" + domain[i] + ","; var p5 = firstname.charAt(0) + "." + lastname + "@" + domain[i] + ","; var p6 = firstname + lastname.charAt(0) + "@" + domain[i] + ","; var p7 = firstname + "." + lastname.charAt(0) + "@" + domain[i] + ","; var p8 = firstname.charAt(0) + lastname.charAt(0) + "@" + domain[i] + ","; var p9 = firstname + "_" + lastname + "@" + domain[i] + ","; var p10 = firstname.charAt(0) + "_" + lastname + "@" + domain[i] + ","; var p11 = lastname + firstname + "@" + domain[i] + ","; var p12 = lastname + "." + firstname + "@" + domain[i] + ","; var p13 = lastname + firstname.charAt(0) + "@" + domain[i] + ","; var p14 = lastname + "." + firstname.charAt(0) + "@" + domain[i]; var addresses = p1 + " " + p2 + " " + p3 + " " + p4 + " " + p5 + " " + p6 + " " + p7 + " " + p8 + " " + p9 + " " + p10 + " " + p11 + " " + p12 + " " + p13 + " " + p14; arr.push(addresses) } } var innertext = document.getElementById("results"); innertext.innerHTML = arr; } for else part和if循环并添加了一个保存结果的数组。然后我使用{}函数填充我的数组,最后一步是将push设置为数组的内容