diff --git a/app/code/Magento/Customer/view/frontend/templates/form/login.phtml b/app/code/Magento/Customer/view/frontend/templates/form/login.phtml index 16206525aa53b..77e250c5de923 100644 --- a/app/code/Magento/Customer/view/frontend/templates/form/login.phtml +++ b/app/code/Magento/Customer/view/frontend/templates/form/login.phtml @@ -42,3 +42,13 @@ + + diff --git a/app/code/Magento/Customer/view/frontend/web/js/trim-username.js b/app/code/Magento/Customer/view/frontend/web/js/trim-username.js new file mode 100644 index 0000000000000..1b6aab6086853 --- /dev/null +++ b/app/code/Magento/Customer/view/frontend/web/js/trim-username.js @@ -0,0 +1,65 @@ +/** + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +define([ + 'jquery' +], function ($) { + 'use strict'; + + $.widget('mage.trimUsername', { + options: { + cache: {}, + formSelector: 'form', + emailSelector: 'input[type="email"]' + }, + + /** + * Widget initialization + * @private + */ + _create: function () { + // We need to look outside the module for backward compatibility, since someone can already use the module. + // @todo Narrow this selector in 2.3 so it doesn't accidentally finds the email field from the + // newsletter email field or any other "email" field. + this.options.cache.email = $(this.options.formSelector).find(this.options.emailSelector); + this._bind(); + }, + + /** + * Event binding, will monitor change, keyup and paste events. + * @private + */ + _bind: function () { + if (this.options.cache.email.length) { + this._on(this.options.cache.email, { + 'change': this._trimUsername, + 'keyup': this._trimUsername, + 'paste': this._trimUsername + }); + } + }, + + /** + * Trim username + * @private + */ + _trimUsername: function () { + var username = this._getUsername().trim(); + + this.options.cache.email.val(username); + }, + + /** + * Get username value + * @returns {*} + * @private + */ + _getUsername: function () { + return this.options.cache.email.val(); + } + }); + + return $.mage.trimUsername; +});