如何将输入的长度设置为8或14个字符?

时间:2016-05-19 08:59:34

标签: javascript jquery

我试图使用以下代码:

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

似乎必须有某种方式让它发挥作用?

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

2 个答案:

答案 0 :(得分:4)

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

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

这就是您的代码的样子:

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

铁路图:

Regex Visualization

说明

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

答案 1 :(得分:1)

管道后你错过了.

<input pattern=".{8}|.{14}">