【问题标题】:Regex pattern should allow only integers from 0 to 100 not allow decimal points正则表达式模式应该只允许从 0 到 100 的整数,不允许小数点
【发布时间】:2016-11-07 15:04:43
【问题描述】:

我有一个输入框,我需要的是它应该只允许从 0 到 100 的值并且它不应该允许输入小数精度应该只允许像 10、25、...等整数而不是 10.5、0.5。 ..等等

我尝试如下:

<input   id="discount" name="discount"  placeholder="Discount" autocomplete="off" type="number" inputmode="numeric" pattern="[0-9]*" value="0" maxlength="2">

FIDDLE

需要修改pattern="[0-9]*"

请指导我。

【问题讨论】:

    标签: regex input integer


    【解决方案1】:

    如果您尝试限制数字范围,最好的方法是提供minmax 属性。此外,如果您希望允许 100 个,则需要增加 maxlength

    input#discount:invalid {
      background-color: red;
    }
     <input id="discount" name="discount"  placeholder="Discount" 
            autocomplete="off" type="number" inputmode="numeric"
            min="0" max="100" maxlength="3"
            pattern="^([0-9]|[1-9][0-9]|100)$">

    【讨论】:

      【解决方案2】:

      它应该是“^[0-9]*$”,为你的模式添加开始和结束指示符

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-26
      • 2020-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-22
      • 1970-01-01
      相关资源
      最近更新 更多