How to count the number of characters without "-" and "()"?

I have an input field for the phone numbers and field length counter. How can I ignore "-", spaces, and "()" in the field when I count its length? I mean, if the input value looks like "(099) 99-99", its length when I call val().length should be 7, and not 11.

var phoneInput = getElementById("phone");
var counter = getElementById("lengthCounter");


Answers 1

  • Check this out! no need to write many lines of code.

    function count() {
        var x = document.getElementById("phonenumber");
        var count = x.value.replace(/[^0-9]/g,"").length;
        $("#displaycount").text("Number Count "+count);
    <script src=""></script>
    Enter your Number: <input type="text" placeholder="example (99) 99-999" id="phonenumber" onkeyup="count()">
    <div id="displaycount"></div>

Related Articles