aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/bootstrap/tfa.js
diff options
context:
space:
mode:
authorJan-Hendrik Willms <tleilax+github@gmail.com>2021-07-22 16:07:19 +0200
committerJan-Hendrik Willms <tleilax+github@gmail.com>2021-07-22 16:19:12 +0200
commita3da1483a9e689846179159355badfec8073dbec (patch)
tree770dcca6bdf5f6f2a11b0e7fcbbeda6919a3fc52 /resources/assets/javascripts/bootstrap/tfa.js
current code from svn, revision 62608
Diffstat (limited to 'resources/assets/javascripts/bootstrap/tfa.js')
-rw-r--r--resources/assets/javascripts/bootstrap/tfa.js42
1 files changed, 42 insertions, 0 deletions
diff --git a/resources/assets/javascripts/bootstrap/tfa.js b/resources/assets/javascripts/bootstrap/tfa.js
new file mode 100644
index 0000000..4880825
--- /dev/null
+++ b/resources/assets/javascripts/bootstrap/tfa.js
@@ -0,0 +1,42 @@
+$(document).on('keyup', '.tfa-code-input input', function (event) {
+ this.value = this.value.replace(/^D/g, '');
+ if (event.keyCode === 8) {
+ $(this).prev('input').focus();
+ if (this.value.length === 0) {
+ $(this).prev('input').val('');
+ }
+ } else if (event.keyCode === 46) {
+ $(this).nextAll('input:not(:hidden)').each(function () {
+ $(this).prev().val(this.value);
+ this.value = '';
+ });
+ } else if (event.keyCode === 37) {
+ $(this).prev('input').focus();
+ } else if (event.keyCode === 39) {
+ $(this).next('input').focus();
+ } else if (event.key >= '0' && event.key <= '9') {
+ this.value = event.key;
+ $(this).next('input').focus();
+ } else if (event.keyCode === 36) {
+ $(this).parent().find('input:not(:hidden):first').focus();
+ }
+}).on('keydown', '.tfa-code-input', function (event) {
+ if (event.key >= '0' && event.key <= '9') {
+ this.value = '';
+ event.preventDefault();
+ }
+}).on('paste', '.tfa-code-input input', function (event) {
+ this.value = '';
+ $(this).one('input', function () {
+ const pastedValue = this.value.trim();
+ if (!pastedValue.match(/^\d{6}$/)) {
+ return;
+ }
+
+ const container = $(this).closest('.tfa-code-input');
+ for (let i = 0; i < 6; i += 1) {
+ $(`input:eq(${i})`, container).val(pastedValue.substr(i, 1))
+ }
+ $('input:last', container).focus();
+ });
+});