Jquery: 在 focusout 上更改输入文本值

Jquery: change input text value on focusout

我有如下字段

输入文本固定为 11 位。

现在,当焦点移开时,我希望输入文本像下面这样更改

123-456-789-01

如何使用 Jquery

实现此目的

谁能指导我

您可以使用下面的正则表达式来格式化您的 phone phone = phone.replace(/(\d{3})(\d{3})(\d{3})(\d{2})/, "---"); 并设置 maxlength=11

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

  function phoneFormat() {
    phone = phone.replace(/[^0-9]/g, '');
    phone = phone.replace(/(\d{3})(\d{3})(\d{3})(\d{2})/, "---");
    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})/, "---");
    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"  />