-3

I am formatting the phone field. I get the value of the element and check to see if it 3 characters. The problem comes when the value ends with two zeros. The length of the var is off if it ends in two zeros.

Example:
120 value comes in at 3
1200 value also comes in at 3

//phone format
function updatephone(x) {
    var phlen = document.getElementById("fphone").value;
    if (phlen.length < 12) {
        if (phlen.length == 3) {
            document.getElementById('fphone').value=document.getElementById('fphone').value + -x;
        }
        else if (phlen.length == 7) {
            document.getElementById('fphone').value=document.getElementById('fphone').value + -x;
        }
        else {
            document.getElementById('fphone').value=document.getElementById('fphone').value + x;
        }
    }
}
Jay
  • 1
  • 1

1 Answers1

0

Here its code its working prefect

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<input id='phone' oninput="test()" type="number" />
<input id='result'  />

  <script>
    function test()
    {
      var v=document.getElementById("phone").value;

      if(v.length<12)
        {
          if(v.length==3)
            {
              document.getElementById("result").value="3";
            }else 
              {
                document.getElementById("result").value="";

              }


        }else if(String(v).Lenght>=12)
          {
            alert("Wrong")
          }

    }

  </script>
</body>
</html>
rtraees
  • 312
  • 2
  • 15
  • This is almost exact code I have yet on the iPad Safari it does not format a number with 00. Try it with all zeros – Jay Jun 12 '17 at 01:53
  • try to use String(v) function before getting string length. – rtraees Jun 12 '17 at 07:52
  • Nope.. no joy. The script won't even run with string(v).value. – Jay Jun 12 '17 at 11:24
  • Using document.getElementId(). When adding -0 to 0 it does not add the dash but it does add the zero – Jay Jun 12 '17 at 11:25