Create username from email address provided – JS

Share Now

#HTML PART

<form action="#" method="post" id="test">
    <label for="email">E-mail
        <input type="text" name="email" id="email" />
    </label>
    <input type="submit" name="submit" value="Create username"/>
</form>

#JS & jQuery Part

var createUserName = function(value) {
    var parts = value.split('@');
    var user = parts[0];
    var username = user.replace(/[^a-z\d]+/ig, '');
    return username;
};


$('#test').submit(function(e) {
    e.preventDefault();
    var $value = $('#email').val();
    var $username = createUserName($value);
    document.getElementById('p_name').value = $username;
    alert($username);
});
Picture of Nahid Mahamud

Nahid Mahamud

Web Developer | Graphic Designer | WordPress & Woo-commerce Expert