jQuery Mask插件不能以字母“ S”开头

时间:2019-02-11 15:24:31

标签: javascript jquery jquery-mask

我想对输入使用掩码,以SP以及PT开头。 PT输入正常,但SP无效。请注意,这不是我的真实代码,仅是示例。

$('#partshort').mask('SP-00000000000');
$('#svpshort').mask('PT-00000000000');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="http://igorescobar.github.io/jQuery-Mask-Plugin/js/jquery.mask.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<div class="container">
  <h3>Problems with jQuery Mask Plugin?</h3>
  <p>Exemplify your problem here :-)</p>
  <hr />
  <form role="form">
    <div class="form-group">
      <label for="date-field">Example 1</label>
      <input type="text" class="form-control" id="partshort">
    </div>
    <div class="form-group">
      <label for="ip-field">Example 2</label>
      <input type="text" class="form-control" id="svpshort">
    </div>
  </form>
</div>

这里是示例:http://jsfiddle.net/1wjbysnm/

1 个答案:

答案 0 :(得分:2)

问题是因为Mask库中的S字符具有特殊含义:

  

默认情况下,jQuery Mask Plugin仅识别逻辑数字A(数字和字母)和S(A-Za-z),但是您可以扩展或修改此行为

https://igorescobar.github.io/jQuery-Mask-Plugin/docs.html#translation

要解决您的问题,您只需删除S字符转换,使其表现为任何标准字符即可:

$('#partshort').mask('SP-00000000000', {
  translation: { S: null }
});
$('#svpshort').mask('PT-00000000000');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="http://igorescobar.github.io/jQuery-Mask-Plugin/js/jquery.mask.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<div class="container">
  <h3>Problems with jQuery Mask Plugin?</h3>
  <p>Exemplify your problem here :-)</p>
  <hr />
  <form role="form">
    <div class="form-group">
      <label for="date-field">Example 1</label>
      <input type="text" class="form-control" id="partshort">
    </div>
    <div class="form-group">
      <label for="ip-field">Example 2</label>
      <input type="text" class="form-control" id="svpshort">
    </div>
  </form>
</div>