Skip to content

Commit 9ecb84b

Browse files
committed
Trim username on customer account login page
1 parent 44502ff commit 9ecb84b

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

app/code/Magento/Customer/view/frontend/requirejs-config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var config = {
99
checkoutBalance: 'Magento_Customer/js/checkout-balance',
1010
address: 'Magento_Customer/address',
1111
changeEmailPassword: 'Magento_Customer/change-email-password',
12+
trimUsername: 'Magento_Customer/js/trim-username',
1213
passwordStrengthIndicator: 'Magento_Customer/js/password-strength-indicator',
1314
zxcvbn: 'Magento_Customer/js/zxcvbn',
1415
addressValidation: 'Magento_Customer/js/addressValidation'

app/code/Magento/Customer/view/frontend/templates/form/login.phtml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,13 @@
4242
</form>
4343
</div>
4444
</div>
45+
46+
<script type="text/x-magento-init">
47+
{
48+
".field.email": {
49+
"trimUsername": {
50+
"formSelector": "form.form-login"
51+
}
52+
}
53+
}
54+
</script>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
define([
7+
'jquery',
8+
], function ($) {
9+
'use strict';
10+
11+
$.widget('mage.trimUsername', {
12+
options: {
13+
cache: {},
14+
formSelector: 'form',
15+
emailSelector: 'input[type="email"]'
16+
},
17+
18+
/**
19+
* Widget initialization
20+
* @private
21+
*/
22+
_create: function () {
23+
// We need to look outside the module for backward compatibility, since someone can already use the module.
24+
// @todo Narrow this selector in 2.3 so it doesn't accidentally finds the the email field from the
25+
// newsletter email field or any other "email" field.
26+
this.options.cache.email = $(this.options.formSelector).find(this.options.emailSelector);
27+
this._bind();
28+
},
29+
30+
/**
31+
* Event binding, will monitor change, keyup and paste events.
32+
* @private
33+
*/
34+
_bind: function () {
35+
if (this.options.cache.email.length) {
36+
this._on(this.options.cache.email, {
37+
'change': this._trimUsername,
38+
'keyup': this._trimUsername,
39+
'paste': this._trimUsername
40+
});
41+
}
42+
},
43+
44+
/**
45+
* Trim username
46+
* @private
47+
*/
48+
_trimUsername: function () {
49+
var username = this._getUsername().trim();
50+
51+
this.options.cache.email.val(username);
52+
},
53+
54+
/**
55+
* Get username value
56+
* @returns {*}
57+
* @private
58+
*/
59+
_getUsername: function () {
60+
return this.options.cache.email.val();
61+
}
62+
});
63+
64+
return $.mage.trimUsername;
65+
});

0 commit comments

Comments
 (0)