“手机号码输入控件“实现

<! DOCTYPE html>
< html>
    < head>
       < meta charset= "utf-8" />
       < title>手机号码格式化</title>
       
       < script type= "text/javascript" >
            function format_mobile( event){
               
               setTimeout( function(value , whtchKey ){
                   var value = event.target.value;
                   var whitchKey = event.keyCode || event.charCode || event.ctrlKey; 
                   // android输入法只能检测到8
                   if (whitchKey == 8){
                       //console.log("删除前:("+value+")");
                       //mobile = value.split('');
                       //console.log("转换后:("+mobile+")");
                       //value = mobile.pop();
                       value = value.substring( 0, value.length);
                       //console.log("删除后:("+value+")");
                       event.target.value = value;
                  } else {
                       var mobile = [];
                      value = value.replace( /[^\d]/g , '');
                       for (var i = 0 ; i < value.length; i ++){
                          mobile.push(value[i]);
                           if ((2 == i) || (6 == i)){
                              mobile.push( '  ');
                          }
                      }
                       event.target.value = mobile.join( '');
                  }
               }, 10);
           }
        </script>
    </ head>
    < body>
       < input type= "tel" id= "mobileid" name="mobileid" onkeyup ="javascript :format_mobile( event)" value=""
            maxlength="15" style=" width: 100%; height: 30px; font-size: 20px; "/>
    </ body>
</ html>

猜你喜欢

转载自aa860326.iteye.com/blog/2253144