Search code examples
javascriptjqueryhtmlformskeyup

Filter value on .keyup method JQuery


I have two fields (username and email) and I want to auto-fill the username with the content of the email field, because username is a hidden input.

I just need the contentent before @domain.com. Right now I'm using the .keyup() but I can use the .change() too.

How can I only get the content before @domain.com?

$('#id_email').keyup(function () {
    $('#id_username').val($(this).val());
});

EDIT: All answers Works perfectly, thank you so much.


Solution

  • Kinda simplistic but can you do something like

    var email = "test@example.com"; var username = email.split('@')[0];

    so:

    <script>
        $('#id_email').change(function () {
            var email = $(this).val();
            $('#id_username').val(email.split('@')[0]);
        });
    </script>

    It will work but, of course, assumes a valid email address.