我如何请求在输入元素中输入一次特定数字(例如 0)?

How could I request a certain digit (e.g. 0) to be entered once in an input element?

我如何请求用户在输入元素中插入特定次数的特定数字?

我需要请求他们在文本或密码字段中输入一次某个数字,比如数字 0,以及 1-4 之间的任何其他数字。

我如何使用 HTML5 或任何其他方式做到这一点?模式是什么?

如果我有这个代码:

<td><input type="password" name="password" maxlength="4" pattern="\d+" title="3 numbers only, with 1 zero only" placeholder="3 numbers only, with 1 zero only"  value="<?php echo htmlentities($password); ?>" /></td>

然后,我需要的模式作为示例:("x" 是从 1 到 4 的任意数字)

xxx0
x0xx
0xxx
xx0x

以及其他可能的组合。

再次注意:

如果我没理解错的话,用户必须输入一个 4 位数字。在数字中必须有一个零(不能更多),但他们可以根据需要多次使用其他数字 (1-4)。这是我想出的:

^[1-4]{0,3}0[1-4]{0,3}$

您可以在此处查看有效的 PHP 示例:PHPLiveRegEx.com Example

显然,您的测试必须在服务器端或在 javascript 中进行,但上述模式会起作用。

编辑

本节与题目无关,只是对提问者帮助解释正则表达式的快速帮助。链接转到 PHPLiveRegEx.com 或 RegEx101.com 上的工作示例。 RegEx101.com 包含链接,因为它足以突出显示文本中的匹配项(示例在其他方面相同)。

  1. Simple example with the regular expression Cat (Same example at RegEx101.com)
  2. Simple example with the regular expression [Cat] (Same example at RegEx101.com)
  3. Example of finding a repeating pattern (Same example at RegEx101.com)
  4. Combination example using concepts above (Same example at RegEx101.com)
  5. Finale example with asterisk. (Same example at RegEx101.com)

唯一要记住的另一件事是,不同的语言对正则表达式的实现方式不同,因此适用于 PHP 的方法可能不适用于另一种语言。通常只需要一些小的调整。最重要的是随便玩玩(我建议使用我为上述示例链接的网站)。