catching the 10th character properly with js












0














I'm trying to automatically convert a phone number typed in an input box to (xxx) xxx-xxxx format where x is the phone number someone types in. I can actually get it working when the input box loses focus but I'm trying to also change it once the 10th character is typed in the input. It's working but not until you click the 11th character and I cannot figure out why.



<input type="tel" class='tel' placeholder="(555) 555-1212" maxlength=10>
<div class="result"></div>

var convert_phone = function () {
var phone_num = $('.tel').val();
var phone_check = phone_num.search(/^(?d{3}D*d{3}D*d{4}$/);

if (phone_check == 0) {
var parts = phone_num.match(/^(?(d{3})D*(d{3})D*(d{4})$/);
$('.tel').val('('+parts[1]+') '+parts[2]+'-'+parts[3]);
}
}

$('.tel').on('keypress', function () {
var current_val = $(this).val().length;

$('.result').text(current_val);
if (current_val == 10) {
convert_phone();
}
})

$('.tel').on('blur', function() {
convert_phone();
})


JSFiddle: https://jsfiddle.net/s6frnp2k/3/
The result div is just there to show click value and isn't needed for the final version.










share|improve this question




















  • 2




    change 'keypress' to 'keyup', fiddle jsfiddle.net/s6frnp2k/4
    – Karthik Ganesan
    Nov 20 at 21:06












  • Since this in an input element, why not listen for the input event instead of keyboard presses? But, even then you will have some issues, since you are not handling the case of someone hitting delete or backspace. Moreover, the new length of your phone number will exceed the maxlength of 10 with the added characters
    – wlh
    Nov 20 at 21:13










  • @wlh I started down that path and realized the same thing.
    – Jason Taylor
    Nov 20 at 21:17
















0














I'm trying to automatically convert a phone number typed in an input box to (xxx) xxx-xxxx format where x is the phone number someone types in. I can actually get it working when the input box loses focus but I'm trying to also change it once the 10th character is typed in the input. It's working but not until you click the 11th character and I cannot figure out why.



<input type="tel" class='tel' placeholder="(555) 555-1212" maxlength=10>
<div class="result"></div>

var convert_phone = function () {
var phone_num = $('.tel').val();
var phone_check = phone_num.search(/^(?d{3}D*d{3}D*d{4}$/);

if (phone_check == 0) {
var parts = phone_num.match(/^(?(d{3})D*(d{3})D*(d{4})$/);
$('.tel').val('('+parts[1]+') '+parts[2]+'-'+parts[3]);
}
}

$('.tel').on('keypress', function () {
var current_val = $(this).val().length;

$('.result').text(current_val);
if (current_val == 10) {
convert_phone();
}
})

$('.tel').on('blur', function() {
convert_phone();
})


JSFiddle: https://jsfiddle.net/s6frnp2k/3/
The result div is just there to show click value and isn't needed for the final version.










share|improve this question




















  • 2




    change 'keypress' to 'keyup', fiddle jsfiddle.net/s6frnp2k/4
    – Karthik Ganesan
    Nov 20 at 21:06












  • Since this in an input element, why not listen for the input event instead of keyboard presses? But, even then you will have some issues, since you are not handling the case of someone hitting delete or backspace. Moreover, the new length of your phone number will exceed the maxlength of 10 with the added characters
    – wlh
    Nov 20 at 21:13










  • @wlh I started down that path and realized the same thing.
    – Jason Taylor
    Nov 20 at 21:17














0












0








0







I'm trying to automatically convert a phone number typed in an input box to (xxx) xxx-xxxx format where x is the phone number someone types in. I can actually get it working when the input box loses focus but I'm trying to also change it once the 10th character is typed in the input. It's working but not until you click the 11th character and I cannot figure out why.



<input type="tel" class='tel' placeholder="(555) 555-1212" maxlength=10>
<div class="result"></div>

var convert_phone = function () {
var phone_num = $('.tel').val();
var phone_check = phone_num.search(/^(?d{3}D*d{3}D*d{4}$/);

if (phone_check == 0) {
var parts = phone_num.match(/^(?(d{3})D*(d{3})D*(d{4})$/);
$('.tel').val('('+parts[1]+') '+parts[2]+'-'+parts[3]);
}
}

$('.tel').on('keypress', function () {
var current_val = $(this).val().length;

$('.result').text(current_val);
if (current_val == 10) {
convert_phone();
}
})

$('.tel').on('blur', function() {
convert_phone();
})


JSFiddle: https://jsfiddle.net/s6frnp2k/3/
The result div is just there to show click value and isn't needed for the final version.










share|improve this question















I'm trying to automatically convert a phone number typed in an input box to (xxx) xxx-xxxx format where x is the phone number someone types in. I can actually get it working when the input box loses focus but I'm trying to also change it once the 10th character is typed in the input. It's working but not until you click the 11th character and I cannot figure out why.



<input type="tel" class='tel' placeholder="(555) 555-1212" maxlength=10>
<div class="result"></div>

var convert_phone = function () {
var phone_num = $('.tel').val();
var phone_check = phone_num.search(/^(?d{3}D*d{3}D*d{4}$/);

if (phone_check == 0) {
var parts = phone_num.match(/^(?(d{3})D*(d{3})D*(d{4})$/);
$('.tel').val('('+parts[1]+') '+parts[2]+'-'+parts[3]);
}
}

$('.tel').on('keypress', function () {
var current_val = $(this).val().length;

$('.result').text(current_val);
if (current_val == 10) {
convert_phone();
}
})

$('.tel').on('blur', function() {
convert_phone();
})


JSFiddle: https://jsfiddle.net/s6frnp2k/3/
The result div is just there to show click value and isn't needed for the final version.







javascript jquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 22:58









wlh

1,6221722




1,6221722










asked Nov 20 at 21:02









Jason Taylor

313




313








  • 2




    change 'keypress' to 'keyup', fiddle jsfiddle.net/s6frnp2k/4
    – Karthik Ganesan
    Nov 20 at 21:06












  • Since this in an input element, why not listen for the input event instead of keyboard presses? But, even then you will have some issues, since you are not handling the case of someone hitting delete or backspace. Moreover, the new length of your phone number will exceed the maxlength of 10 with the added characters
    – wlh
    Nov 20 at 21:13










  • @wlh I started down that path and realized the same thing.
    – Jason Taylor
    Nov 20 at 21:17














  • 2




    change 'keypress' to 'keyup', fiddle jsfiddle.net/s6frnp2k/4
    – Karthik Ganesan
    Nov 20 at 21:06












  • Since this in an input element, why not listen for the input event instead of keyboard presses? But, even then you will have some issues, since you are not handling the case of someone hitting delete or backspace. Moreover, the new length of your phone number will exceed the maxlength of 10 with the added characters
    – wlh
    Nov 20 at 21:13










  • @wlh I started down that path and realized the same thing.
    – Jason Taylor
    Nov 20 at 21:17








2




2




change 'keypress' to 'keyup', fiddle jsfiddle.net/s6frnp2k/4
– Karthik Ganesan
Nov 20 at 21:06






change 'keypress' to 'keyup', fiddle jsfiddle.net/s6frnp2k/4
– Karthik Ganesan
Nov 20 at 21:06














Since this in an input element, why not listen for the input event instead of keyboard presses? But, even then you will have some issues, since you are not handling the case of someone hitting delete or backspace. Moreover, the new length of your phone number will exceed the maxlength of 10 with the added characters
– wlh
Nov 20 at 21:13




Since this in an input element, why not listen for the input event instead of keyboard presses? But, even then you will have some issues, since you are not handling the case of someone hitting delete or backspace. Moreover, the new length of your phone number will exceed the maxlength of 10 with the added characters
– wlh
Nov 20 at 21:13












@wlh I started down that path and realized the same thing.
– Jason Taylor
Nov 20 at 21:17




@wlh I started down that path and realized the same thing.
– Jason Taylor
Nov 20 at 21:17

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401485%2fcatching-the-10th-character-properly-with-js%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401485%2fcatching-the-10th-character-properly-with-js%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Feedback on college project

Futebolista

Albești (Vaslui)