Skip to content

Commit 8b16266

Browse files
add "enterClicked" option method
1 parent e275a0b commit 8b16266

File tree

4 files changed

+54
-15
lines changed

4 files changed

+54
-15
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,14 @@ $('#otp_target').otpdesigner({
234234
In these examples, the OTP designer is initialized inside the otp_target element with different configurations. The first example demonstrates the basic usage without custom options, while the second example shows a customized OTP input with larger input fields and a length of 8 digits. The third example demonstrates using options as an object to customize the OTP input.
235235
## Options
236236

237-
| **Option** | **Type** | **Default** | **Description** |
238-
|---------------------------|----------|--------------|----------------------------------------------------------------------------------|
239-
| **`length`** | Integer | 6 | The number of OTP input fields. |
240-
| **`onluNumbers`** | Boolean | false | Allow only numeric input. |
241-
| **`inputsClasses`** | String | "" | Additional CSS classes to apply to the OTP input fields. |
242-
| **`inputsParentClasses`** | String | "" | Additional CSS classes to apply to the parent container of the OTP input fields. |
243-
| **`typingDone`** | Function | (code) => {} | A callback function executed when the user completes typing the OTP. |
237+
| **Option** | **Type** | **Default** | **Description** |
238+
|---------------------------|----------|-------------|--------------------------------------------------------------------------------------|
239+
| **`length`** | Integer | 6 | The number of OTP input fields. |
240+
| **`onluNumbers`** | Boolean | false | Allow only numeric input. |
241+
| **`inputsClasses`** | String | "" | Additional CSS classes to apply to the OTP input fields. |
242+
| **`inputsParentClasses`** | String | "" | Additional CSS classes to apply to the parent container of the OTP input fields. |
243+
| **`typingDone`** | Function | null | A callback function executed when the user completes typing the OTP. |
244+
| **`enterClicked`** | Function | null | A callback function executed when the user click on Enter key when he's done typing. |
244245

245246
## Methods
246247
The OTP Designer jQuery Plugin provides the following method:

dist/otpdesigner.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,8 @@ const otpdesigner = function (options = {}, ...args) {
578578
onlyNumbers: false,
579579
inputsClasses: '',
580580
inputsParentClasses: '',
581-
typingDone : function () {}
581+
enterClicked: null,
582+
typingDone : null
582583
},
583584
options
584585
);
@@ -633,6 +634,14 @@ const otpdesigner = function (options = {}, ...args) {
633634
if (event.key === "Backspace") {
634635
$inputs[i].value = "";
635636
if (i !== 0) $inputs[i - 1].focus();
637+
} else if (event.key === "Enter" && i === $inputs.length - 1 && $inputs[i].value !== "") {
638+
event.preventDefault();
639+
collectOtpCode(data, false);
640+
if (settings.enterClicked != null) {
641+
settings.enterClicked();
642+
}
643+
loseFocus(data);
644+
return;
636645
} else {
637646
if (event.keyCode > 95 && event.keyCode < 106) {
638647
$inputs[i].value = event.key;
@@ -679,15 +688,25 @@ let stringToBool = function (s) {
679688
return ['true', 'TRUE', '1'].includes(s.toString());
680689
}
681690

682-
let collectOtpCode = (data) => {
691+
let collectOtpCode = (data, typingDone = true) => {
683692
let $inputs = $('#otp_' + data.idSuffix).find('.otp-input');
684693
let code = '';
685694
$inputs.each(function (i, e) {
686695
code += $(e).val().trim();
687696
});
688697
$('#otp_hidden_' + data.idSuffix).val(code);
689-
if (code.length === $inputs.length) data.settings.typingDone(code);
698+
if (code.length === $inputs.length && typingDone) {
699+
if (data.settings.typingDone != null) {
700+
data.settings.typingDone(code);
701+
}
702+
loseFocus(data);
703+
}
690704
}
705+
706+
let loseFocus = (data) => {
707+
if (data.settings.enterClicked != null) return;
708+
$('.otp-input:focus').blur();
709+
};
691710
;// CONCATENATED MODULE: ./index.js
692711

693712

dist/otpdesigner.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)