【问题标题】:I want to use this pattern to allow only these string like '#00000001' [duplicate]我想使用这种模式只允许像'#00000001'这样的字符串[重复]
【发布时间】:2021-03-29 19:24:14
【问题描述】:

我的代码是

let bikeno = $('input:text[name=bikeno]').val().trim();
let regBikeno = /^#\d{8}*$/;

if(!regBikeno.test(bikeno)) {
    alert("자전거 일련번호를 정확히 입력해 주세요.");
    return false;
}

我想允许这种模式 (#00000001)。

允许的字符串以# 开头,由八个数字组成。

【问题讨论】:

  • 去掉量词^#\d{8}$之后的*

标签: javascript html jquery regex spring


【解决方案1】:

您需要删除 * 以仅允许 # 后面的 8 位数字:

let regBikeno = /^#\d{8}$/;

示例:

$('#check').on('click', function() {
  let bikeno = $('input:text[name=bikeno]').val().trim();
  let regBikeno = /^#\d{8}$/;

  if (!regBikeno.test(bikeno)) {
    console.log("DOESN'T MATCH");
  } else {
    console.log("MATCH");
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" name="bikeno" value="#33333333">

<button id="check">CHECK</button>

【讨论】:

  • 它的工作!谢谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-09
  • 1970-01-01
  • 2019-09-19
  • 2017-04-06
相关资源
最近更新 更多