0

I am trying to implement phone no. masking for Mobile website. And I could not use jquery mask plugin as it was not working properly in devices. Below implementation is working fine in Android Chrome Mobile Browser. But in case of Android default browser, when I try to delete all numbers using backspace key, last some(2/3) numbers/digits are not clearing. I guess this.value += "-"; is not updating some textbox property. Phone No. Format is (ex. ###-###-####)

HTML

<input id="phoneNo" type="tel" maxlength="12" placeholder="Phone"/>

JavaScript

var phoneNo = document.getElementById("phoneNo");
phoneNo.addEventListener("textInput",function(){
if(this.value.toString().length==3 || this.value.toString().length==7) {
   this.value += "-";            
}
});
Darshan
  • 750
  • 7
  • 19

2 Answers2

0

Have a look at the jQuery on() method as not all browsers support addEventListener which I am guessing is what the problem is.

http://api.jquery.com/on/

Gabriel Sadaka
  • 1,748
  • 1
  • 15
  • 19
0

Mr. Darshan please have a look at http://jsfiddle.net/2dJAN/51/

In my app also i am using these kind feature. This example

  1. Will allow the user to type only integers in field
  2. This will automatically add the "-" between numbers like 123-456-7890 format
  3. You can delete all the numbers inside the field.

    $("#fax_field").on("keyup", function(event) {
            var limitField = $(this).val().trim().length;
            var limit = "12"
            this.value = this.value.replace(/[^0-9_\-\.]/g, '');
            if (event.keyCode != 8) {
              if (limitField == 3) {
                var fax_value = $(this).val().trim().concat('-');
                $("#fax_field").val(fax_value);
              } else if (limitField == 7) {
                var fax_value = $(this).val().trim().concat('-');
                $("#fax_field").val(fax_value);
              }
            }
            if (limitField > limit) {
                $("#fax_field").val($(this).val().trim().substring(0, limit));
            }
        });
    

Please try this. Let me know your comments.

vinothini
  • 2,606
  • 4
  • 27
  • 42