E-mail addresses

by Steven Kellow

JavaScript

var email = "john-smith@hotmail.co.uk"
var parts = email.split("@"); // Splits the email at the @ symbol

var username = parts[0]; // Can be used as a username if you'd like, but we'll use it to find names anyway

var delimiters = [".", "-", "_"];  // List of common email name delimiters, feel free to add to it

delimiters.forEach(splitting_loop);

function splitting_loop(element, index, array) { // Checks all the delimiters
  var num = username.indexOf(element); // Gets the position of the delimter
  if (num > -1) { // If there is a delimiter
    fname = username.substring(0, (num)); // Gets data before delimiter
    lname = username.substring(num + 1, (username.length)); // Gets name after delimiter

    fname = fname.toLowerCase(); // Makes it all lower case
    lname = lname.toLowerCase();

    fname = fname.charAt(0).toUpperCase() + fname.slice(1); // Lets make the first letter upper case as well so it looks prettier
    lname = lname.charAt(0).toUpperCase() + lname.slice(1);

  }

}
if (typeof fname !== 'undefined') { // If we've found a delimiter we can use it
  window.alert(fname);
  window.alert(lname);
}