Html 输入类型=“文本”的模式,最小和最大数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30786775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:38:01  来源:igfitidea点击:

pattern for input type="text" with min and max number

htmlregexvalidation

提问by artur47wien

how to write pattern for input type="text" (it can't be number and the validation can't be with JS) that allow me to enter only numbers, min:1 and max:5000?

如何为 input type="text" 编写模式(它不能是数字,并且验证不能用 JS 进行)允许我只输入数字,min:1 和 max:5000?

    <input type="text" name="someName" id="someId" required pattern=""/>

回答by light

Here you go - not an input with type="number"and no JS:

给你 - 不是一个type="number"没有 JS的输入:

<input type="text" name="someName" id="someId" required="required"
 pattern="(5000|([1-4][0-9][0-9][0-9])|([1-9][0-9][0-9])|([1-9][0-9])|[1-9])"/>

The basic pattern is to match 5000 or 4-digit number or 3-digit number or 2-digit number or non-zero 1-digit number.

基本模式是匹配5000或4位数字或3位数字或2位数字或非零1位数字。

If you can accept 0, the pattern can be even simpler:

如果您可以接受 0,则模式可以更简单:

(5000|([1-4]?[0-9]?[0-9]?[0-9]?))

回答by Saumil

You can use the predefined minand maxattribute for input type="number"as follows,

您可以使用预定义minmax属性input type="number"如下,

<input type="number" min=0 max=10>
<input type="submit">

Here is an example

这是一个例子

The error for incorrect input will be displayed when you try to submit the form

当您尝试提交表单时将显示输入错误的错误

回答by Bevanz

Let me know if this works :)

让我知道这个是否奏效 :)

  <input type="number">

HTML Version above!

上面的 HTML 版本!