I have been doing some user testing and noticed that lots of people put in a space after/before their email address, especially if they are ordering from a mobile phone, i.e. " name@domain.com ".

Because of the space, magento shows a red box error on it and some users get confused and don't know what to do. Is there a easy way of trimming the space from the email, through either code or javascript?

Any guide for this will be appreciated.

  • Try this var email = " name@domail.com"; console.log(email.trim()); – Vishnunath Jun 13 '18 at 14:10

Try adding this code to your site: (change #email_address with the ID of your email field)

    var pretrim = jQuery(this).val();

Try to add below code in your file where you want to trim the text:

    document.getElementById("email_address").onblur = function() {
        var val = document.getElementById("email_address").value;
        var str = val.trim();
        document.getElementById("email_address").value = str;

This method will work even when you are not using jQuery.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.