我正在尝试使用以下代码:

<form>
  <input pattern=".{5}|{10}">
      <input type="submit" value="Check"></input>
</form>


看来必须有某种方法使其起作用?

编辑澄清:我不想允许9,10,11,12,13。我只想允许8或14
谢谢,
nb:由于某种原因,我无法使用Jquery Validation插件。

最佳答案

您可以通过以下正则表达式模式指定它:

^.{8}$|^.{14}$


这是您的代码如下所示:

<form>
  <input pattern="^.{8}$|^.{14}$">
  <input type="submit" value="Check"></input>
</form>


铁路图:



描述:

NODE                     EXPLANATION
----------------------------------------------------------------------
 ^                        the beginning of a "line"
----------------------------------------------------------------------
  .{8}                     any character except \n (8 times)
----------------------------------------------------------------------
  $                        before an optional \n, and the end of a
                           "line"
----------------------------------------------------------------------
 |                        OR
----------------------------------------------------------------------
  ^                        the beginning of a "line"
----------------------------------------------------------------------
  .{14}                    any character except \n (14 times)
----------------------------------------------------------------------
  $                        before an optional \n, and the end of a
                           "line"
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------


Demo

09-07 17:36