Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed removing service symbols. Fixed input in middle without formatting. #3

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode
21 changes: 19 additions & 2 deletions phoneinput.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ document.addEventListener("DOMContentLoaded", function () {
formattedInputValue = "";

if (!inputNumbersValue) {
return input.value = "";
return input.value = (e.data == "+") ? "+" : "";
}

if (input.value.length != selectionStart) {
// Editing in the middle of input, not last symbol
if (input.value[0] != '+'){ // Add "+" if input value startswith not "+"
var oldSelectionStart = input.selectionStart
input.value = '+' + input.value;
input.selectionStart = input.selectionEnd = oldSelectionStart + 1
}
if (e.data && /\D/g.test(e.data)) {
// Attempt to input non-numeric symbol
input.value = inputNumbersValue;
Expand Down Expand Up @@ -62,10 +67,22 @@ document.addEventListener("DOMContentLoaded", function () {
input.value = formattedInputValue;
}
var onPhoneKeyDown = function (e) {
// Clear input after remove last symbol
var inputValue = e.target.value.replace(/\D/g, '');
if (e.keyCode == 8 && inputValue.length == 1) {
// Clear input after remove last symbol
e.target.value = "";
} else if ([8, 46].indexOf(e.keyCode) > -1 && inputValue.length > 1) {
// Prevent when removing service symbols
var symToClear
switch (e.keyCode) {
case 8: // BackSpace key
symToClear = e.target.value[e.target.selectionStart - 1];
break;
case 46: // Delete key
symToClear = e.target.value[e.target.selectionStart];
break;
}
if (symToClear && /\D/.test(symToClear)) e.preventDefault();
}
}
for (var phoneInput of phoneInputs) {
Expand Down