简体   繁体   English

Jquery:更改焦点上的输入文本值

[英]Jquery: change input text value on focusout

I have the field like below我有如下字段

在此处输入图像描述

input text is fixed to 11 digit.输入文本固定为 11 位。

Now When focus out I want the input text to be changed like below现在,当焦点出来时,我希望输入文本像下面这样更改

123-456-789-01 123-456-789-01

how can I achive this using Jquery如何使用 Jquery 实现此目的

can anyone guide me谁能指导我

You can use regex below to format your phone phone = phone.replace(/(\d{3})(\d{3})(\d{3})(\d{2})/, "$1-$2-$3-$4");您可以使用下面的正则表达式来格式化您的手机phone = phone.replace(/(\d{3})(\d{3})(\d{3})(\d{2})/, "$1-$2-$3-$4"); and set maxlength =11并设置maxlength =11

$('#phone').focusout(function() {

  function phoneFormat() {
    phone = phone.replace(/[^0-9]/g, '');
    phone = phone.replace(/(\d{3})(\d{3})(\d{3})(\d{2})/, "$1-$2-$3-$4");
    return phone;
  }
  var phone = $(this).val();
  phone = phoneFormat(phone);
  $(this).val(phone);
});

 $('#phone').focusout(function() { function phoneFormat() { phone = phone.replace(/[^0-9]/g, ''); phone = phone.replace(/(\d{3})(\d{3})(\d{3})(\d{2})/, "$1-$2-$3-$4"); return phone; } var phone = $(this).val(); phone = phoneFormat(phone); $(this).val(phone); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <input type="text" value="" id="phone" maxlength="11" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM