pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  • <tfoot id='rf62s'></tfoot>

    <small id='rf62s'></small><noframes id='rf62s'>

    <i id='rf62s'><tr id='rf62s'><dt id='rf62s'><q id='rf62s'><span id='rf62s'><b id='rf62s'><form id='rf62s'><ins id='rf62s'></ins><ul id='rf62s'></ul><sub id='rf62s'></sub></form><legend id='rf62s'></legend><bdo id='rf62s'><pre id='rf62s'><center id='rf62s'></center></pre></bdo></b><th id='rf62s'></th></span></q></dt></tr></i><div class="nh5vp9f" id='rf62s'><tfoot id='rf62s'></tfoot><dl id='rf62s'><fieldset id='rf62s'></fieldset></dl></div>

    • <bdo id='rf62s'></bdo><ul id='rf62s'></ul>

      1. <legend id='rf62s'><style id='rf62s'><dir id='rf62s'><q id='rf62s'></q></dir></style></legend>

        使用純 JavaScript 將電話號碼格式化為用戶類型

        Format a phone number as a user types using pure JavaScript(使用純 JavaScript 將電話號碼格式化為用戶類型)
          <bdo id='a6lUi'></bdo><ul id='a6lUi'></ul>

          <small id='a6lUi'></small><noframes id='a6lUi'>

          1. <tfoot id='a6lUi'></tfoot>

              • <legend id='a6lUi'><style id='a6lUi'><dir id='a6lUi'><q id='a6lUi'></q></dir></style></legend>

                    <tbody id='a6lUi'></tbody>
                  <i id='a6lUi'><tr id='a6lUi'><dt id='a6lUi'><q id='a6lUi'><span id='a6lUi'><b id='a6lUi'><form id='a6lUi'><ins id='a6lUi'></ins><ul id='a6lUi'></ul><sub id='a6lUi'></sub></form><legend id='a6lUi'></legend><bdo id='a6lUi'><pre id='a6lUi'><center id='a6lUi'></center></pre></bdo></b><th id='a6lUi'></th></span></q></dt></tr></i><div class="xph5txb" id='a6lUi'><tfoot id='a6lUi'></tfoot><dl id='a6lUi'><fieldset id='a6lUi'></fieldset></dl></div>
                  本文介紹了使用純 JavaScript 將電話號碼格式化為用戶類型的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我的文檔正文中有一個輸入字段,我需要在用戶鍵入時對其進行格式化.它應該在區號周圍加上括號,然后在三到四位數字之間加上一個破折號.

                  I've got an input field in the body of my document, and I need to format it as the user types. It should have parenthesis around the area code and a dash between the three and four digits after that.

                  例如:(123) 456 - 7890

                  當用戶鍵入時,它應該類似于:

                  As the user types it should look something like:

                  (12
                  (123)
                  (123) 456
                  (123) 456 - 78
                  (123) 456 - 7890

                  推薦答案

                  新的 ES6 答案

                  您仍然可以使用一些簡單的 JavaScript 來做到這一點.

                  HTML

                  New ES6 Answer

                  You can still do this using some simple JavaScript.

                  HTML

                  <input id="phoneNumber" maxlength="16" />
                  

                  JavaScript (ES6)

                  const isNumericInput = (event) => {
                      const key = event.keyCode;
                      return ((key >= 48 && key <= 57) || // Allow number line
                          (key >= 96 && key <= 105) // Allow number pad
                      );
                  };
                  
                  const isModifierKey = (event) => {
                      const key = event.keyCode;
                      return (event.shiftKey === true || key === 35 || key === 36) || // Allow Shift, Home, End
                          (key === 8 || key === 9 || key === 13 || key === 46) || // Allow Backspace, Tab, Enter, Delete
                          (key > 36 && key < 41) || // Allow left, up, right, down
                          (
                              // Allow Ctrl/Command + A,C,V,X,Z
                              (event.ctrlKey === true || event.metaKey === true) &&
                              (key === 65 || key === 67 || key === 86 || key === 88 || key === 90)
                          )
                  };
                  
                  const enforceFormat = (event) => {
                      // Input must be of a valid number format or a modifier key, and not longer than ten digits
                      if(!isNumericInput(event) && !isModifierKey(event)){
                          event.preventDefault();
                      }
                  };
                  
                  const formatToPhone = (event) => {
                      if(isModifierKey(event)) {return;}
                  
                      const input = event.target.value.replace(/D/g,'').substring(0,10); // First ten digits of input only
                      const areaCode = input.substring(0,3);
                      const middle = input.substring(3,6);
                      const last = input.substring(6,10);
                  
                      if(input.length > 6){event.target.value = `(${areaCode}) ${middle} - ${last}`;}
                      else if(input.length > 3){event.target.value = `(${areaCode}) ${middle}`;}
                      else if(input.length > 0){event.target.value = `(${areaCode}`;}
                  };
                  
                  const inputElement = document.getElementById('phoneNumber');
                  inputElement.addEventListener('keydown',enforceFormat);
                  inputElement.addEventListener('keyup',formatToPhone);
                  

                  如果你想擺弄它:
                  https://jsfiddle.net/rafj3md0/

                  免責聲明:
                  值得注意的是,如果您嘗試修改數字的中間,這會有點奇怪,因為瀏覽器在設置元素值后處理插入符號位置的方式.解決這個問題是可行的,但需要比我現在更多的時間,而且有一些圖書館可以處理這樣的事情.

                  Disclaimer:
                  It's worth noting this gets a little weird if you attempt to modify the middle of the number because of the way browsers handle caret placement after you set an element's value. Solving that problem is doable, but would require more time than I have right now, and there are libraries out there that handle things like that.

                  如果您的 HTML 看起來像:
                  <輸入類型=文本"id="phoneNumber"/>

                  If your HTML looks like:
                  <input type="text" id="phoneNumber"/>

                  您的 JavaScript 函數可以是:

                  Your JavaScript function can simply be:

                  // A function to format text to look like a phone number
                  function phoneFormat(input){
                          // Strip all characters from the input except digits
                          input = input.replace(/D/g,'');
                          
                          // Trim the remaining input to ten characters, to preserve phone number format
                          input = input.substring(0,10);
                  
                          // Based upon the length of the string, we add formatting as necessary
                          var size = input.length;
                          if(size == 0){
                                  input = input;
                          }else if(size < 4){
                                  input = '('+input;
                          }else if(size < 7){
                                  input = '('+input.substring(0,3)+') '+input.substring(3,6);
                          }else{
                                  input = '('+input.substring(0,3)+') '+input.substring(3,6)+' - '+input.substring(6,10);
                          }
                          return input; 
                  }
                  

                  當然,您需要一個事件監聽器:

                  Of course, you'll need an event listener:

                  document.getElementById('phoneNumber').addEventListener('keyup',function(evt){
                          var phoneNumber = document.getElementById('phoneNumber');
                          var charCode = (evt.which) ? evt.which : evt.keyCode;
                          phoneNumber.value = phoneFormat(phoneNumber.value);
                  });
                  

                  除非您可以將電話號碼存儲為格式化字符串(我不推薦這樣做),否則您需要在提交值之前清除非數字字符:
                  document.getElementById('phoneNumber').value.replace(/D/g,'');

                  And unless you're okay storing phone numbers as formatted strings (I don't recommend this), you'll want to purge the non-numeric characters before submitting the value with something like:
                  document.getElementById('phoneNumber').value.replace(/D/g,'');

                  如果您想通過獎勵輸入過濾查看此操作,請查看此小提琴:
                  http://jsfiddle.net/rm9vg16m/

                  If you'd like to see this in action with bonus input filtering, check out this fiddle:
                  http://jsfiddle.net/rm9vg16m/

                  // Format the phone number as the user types it
                  document.getElementById('phoneNumber').addEventListener('keyup', function(evt) {
                    var phoneNumber = document.getElementById('phoneNumber');
                    var charCode = (evt.which) ? evt.which : evt.keyCode;
                    phoneNumber.value = phoneFormat(phoneNumber.value);
                  });
                  
                  // We need to manually format the phone number on page load
                  document.getElementById('phoneNumber').value = phoneFormat(document.getElementById('phoneNumber').value);
                  
                  // A function to determine if the pressed key is an integer
                  function numberPressed(evt) {
                    var charCode = (evt.which) ? evt.which : evt.keyCode;
                    if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode < 36 || charCode > 40)) {
                      return false;
                    }
                    return true;
                  }
                  
                  // A function to format text to look like a phone number
                  function phoneFormat(input) {
                    // Strip all characters from the input except digits
                    input = input.replace(/D/g, '');
                  
                    // Trim the remaining input to ten characters, to preserve phone number format
                    input = input.substring(0, 10);
                  
                    // Based upon the length of the string, we add formatting as necessary
                    var size = input.length;
                    if (size == 0) {
                      input = input;
                    } else if (size < 4) {
                      input = '(' + input;
                    } else if (size < 7) {
                      input = '(' + input.substring(0, 3) + ') ' + input.substring(3, 6);
                    } else {
                      input = '(' + input.substring(0, 3) + ') ' + input.substring(3, 6) + ' - ' + input.substring(6, 10);
                    }
                    return input;
                  }

                  Enter a phone number here: <input type="text" id="phoneNumber" onkeypress="return numberPressed(event);" />

                  這篇關于使用純 JavaScript 將電話號碼格式化為用戶類型的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Browserify, Babel 6, Gulp - Unexpected token on spread operator(Browserify,Babel 6,Gulp - 傳播運算符上的意外令牌)
                  Is it possible to pass a flag to Gulp to have it run tasks in different ways?(是否可以將標志傳遞給 Gulp 以使其以不同的方式運行任務?)
                  Why do we need to install gulp globally and locally?(為什么我們需要在全局和本地安裝 gulp?)
                  How to run Gulp tasks sequentially one after the other(如何一個接一個地依次運行 Gulp 任務)
                  Visual Studio 2015 crashes when opening Javascript files(打開 Javascript 文件時 Visual Studio 2015 崩潰)
                  Detect FLASH plugin crashes(檢測 FLASH 插件崩潰)

                • <tfoot id='7aeES'></tfoot>

                    <tbody id='7aeES'></tbody>
                • <small id='7aeES'></small><noframes id='7aeES'>

                      <legend id='7aeES'><style id='7aeES'><dir id='7aeES'><q id='7aeES'></q></dir></style></legend>

                        <i id='7aeES'><tr id='7aeES'><dt id='7aeES'><q id='7aeES'><span id='7aeES'><b id='7aeES'><form id='7aeES'><ins id='7aeES'></ins><ul id='7aeES'></ul><sub id='7aeES'></sub></form><legend id='7aeES'></legend><bdo id='7aeES'><pre id='7aeES'><center id='7aeES'></center></pre></bdo></b><th id='7aeES'></th></span></q></dt></tr></i><div class="5xnv5xr" id='7aeES'><tfoot id='7aeES'></tfoot><dl id='7aeES'><fieldset id='7aeES'></fieldset></dl></div>
                          • <bdo id='7aeES'></bdo><ul id='7aeES'></ul>
                            主站蜘蛛池模板: 大_小鼠elisa试剂盒-植物_人Elisa试剂盒-PCR荧光定量试剂盒-上海一研生物科技有限公司 | 移动厕所租赁|移动卫生间|上海移动厕所租赁-家瑞租赁 | 深圳市宏康仪器科技有限公司-模拟高空低压试验箱-高温防爆试验箱-温控短路试验箱【官网】 | 营养师网,营养师考试时间,报名入口—网站首页 | 防火板_饰面耐火板价格、厂家_品牌认准格林雅 | 雾度仪_雾度计_透光率雾度仪价格-三恩时(3nh)光电雾度仪厂家 | 物流之家新闻网-最新物流新闻|物流资讯|物流政策|物流网-匡匡奈斯物流科技 | 青岛空压机,青岛空压机维修/保养,青岛空压机销售/出租公司,青岛空压机厂家电话 | 磁力抛光机_磁力研磨机_磁力去毛刺机_精密五金零件抛光设备厂家-冠古科技 | 聚合氯化铝_喷雾聚氯化铝_聚合氯化铝铁厂家_郑州亿升化工有限公司 | 2025世界机器人大会_IC China_半导体展_集成电路博览会_智能制造展览网 | 上海网站建设-上海网站制作-上海网站设计-上海做网站公司-咏熠软件 | 北京网络营销推广_百度SEO搜索引擎优化公司_网站排名优化_谷歌SEO - 北京卓立海创信息技术有限公司 | SRRC认证|CCC认证|CTA申请_IMEI|MAC地址注册-英利检测 | 施工电梯_齿条货梯_烟囱电梯_物料提升机-河南大诚机械制造有限公司 | 浙江华锤电器有限公司_地磅称重设备_防作弊地磅_浙江地磅售后维修_无人值守扫码过磅系统_浙江源头地磅厂家_浙江工厂直营地磅 | 手持式3d激光扫描仪-便携式三维立体扫描仪-北京福禄克斯 | 空气能采暖,热泵烘干机,空气源热水机组|设备|厂家,东莞高温热泵_正旭新能源 | 减速机_上海宜嘉减速机 | 点胶机_点胶阀_自动点胶机_智能点胶机_喷胶机_点胶机厂家【欧力克斯】 | LED投光灯-工矿灯-led路灯头-工业灯具 - 山东普瑞斯照明科技有限公司 | _网名词典_网名大全_qq网名_情侣网名_个性网名 | 广州活动策划公司-15+年专业大型公关活动策划执行管理经验-睿阳广告 | 工业洗衣机_工业洗涤设备_上海力净工业洗衣机厂家-洗涤设备首页 bkzzy在职研究生网 - 在职研究生招生信息咨询平台 | 大数据营销公司_舆情监测软件_上海SEO公司-文军营销官网 | 聚合氯化铝厂家-聚合氯化铝铁价格-河南洁康环保科技 | 活性炭-蜂窝-椰壳-柱状-粉状活性炭-河南唐达净水材料有限公司 | 影像测量仪_三坐标测量机_一键式二次元_全自动影像测量仪-广东妙机精密科技股份有限公司 | 电缆接头_防水接头_电缆防水接头_防水电缆接头_上海闵彬 | 沈阳庭院景观设计_私家花园_别墅庭院设计_阳台楼顶花园设计施工公司-【沈阳现代时园艺景观工程有限公司】 | 2025福建平潭岛旅游攻略|蓝眼泪,景点,住宿攻略-趣平潭网 | TPE_TPE热塑性弹性体_TPE原料价格_TPE材料厂家-惠州市中塑王塑胶制品公司- 中塑王塑胶制品有限公司 | 南京精锋制刀有限公司-纵剪机刀片_滚剪机刀片_合金刀片厂家 | 胶辊硫化罐_胶鞋硫化罐_硫化罐厂家-山东鑫泰鑫智能装备有限公司 意大利Frascold/富士豪压缩机_富士豪半封闭压缩机_富士豪活塞压缩机_富士豪螺杆压缩机 | 雷蒙磨,雷蒙磨粉机,雷蒙磨机 - 巩义市大峪沟高峰机械厂 | QQ房产导航-免费收录优秀房地产网站_房地产信息网 | 东莞市海宝机械有限公司-不锈钢分选机-硅胶橡胶-生活垃圾-涡电流-静电-金属-矿石分选机 | 四合院设计_四合院装修_四合院会所设计-四合院古建设计与建造中心1 | 震动筛选机|震动分筛机|筛粉机|振筛机|振荡筛-振动筛分设备专业生产厂家高服机械 | 济南侦探调查-济南调查取证-山东私家侦探-山东白豹调查咨询公司 密集架|电动密集架|移动密集架|黑龙江档案密集架-大量现货厂家销售 | 上海单片机培训|重庆曙海培训分支机构—CortexM3+uC/OS培训班,北京linux培训,Windows驱动开发培训|上海IC版图设计,西安linux培训,北京汽车电子EMC培训,ARM培训,MTK培训,Android培训 |