signal-desktop/js/views/phone-input-view.js

40 lines
1 KiB
JavaScript
Raw Normal View History

/* global libphonenumber, Whisper */
// eslint-disable-next-line func-names
2018-04-27 21:25:04 +00:00
(function() {
'use strict';
2018-04-27 21:25:04 +00:00
window.Whisper = window.Whisper || {};
2018-04-27 21:25:04 +00:00
Whisper.PhoneInputView = Whisper.View.extend({
tagName: 'div',
className: 'phone-input',
templateName: 'phone-number',
initialize() {
2018-04-27 21:25:04 +00:00
this.$('input.number').intlTelInput();
},
events: {
change: 'validateNumber',
keyup: 'validateNumber',
},
validateNumber() {
const input = this.$('input.number');
const regionCode = this.$('li.active')
2018-04-27 21:25:04 +00:00
.attr('data-country-code')
.toUpperCase();
const number = input.val();
const parsedNumber = libphonenumber.util.parseNumber(number, regionCode);
2018-04-27 21:25:04 +00:00
if (parsedNumber.isValidNumber) {
this.$('.number-container').removeClass('invalid');
this.$('.number-container').addClass('valid');
} else {
this.$('.number-container').removeClass('valid');
}
input.trigger('validation');
2015-12-09 21:49:15 +00:00
2018-04-27 21:25:04 +00:00
return parsedNumber.e164;
},
});
})();