Add dash in auto complete phone number

后端 未结 3 1948
一整个雨季
一整个雨季 2020-12-30 11:04

Question:

Is it possible to automatically add a dash \"-\" in auto complete?

Let say I have a phone number field and if someone will type their phone number

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-30 11:23

    You can try this code.

    $(function () {
    
        $('#txtnumber').keydown(function (e) {
           var key = e.charCode || e.keyCode || 0;
           $text = $(this); 
           if (key !== 8 && key !== 9) {
               if ($text.val().length === 3) {
                   $text.val($text.val() + '-');
               }
               if ($text.val().length === 7) {
                   $text.val($text.val() + '-');
               }
    
           }
    
           return (key == 8 || key == 9 || key == 46 || (key >= 48 && key <= 57) || (key >= 96 && key <= 105));
       })
    });
    

    HTML input

    
    

    You can also see in jsfiddle

    https://jsfiddle.net/karthikjsf/38vdpj4f/

提交回复
热议问题