Is there a way to set a min and max length of 15 numbers (numbers only) in my form field?
有没有办法在我的表单字段中设置15个数字(仅限数字)的最小和最大长度?
I've tried:
pattern".{15,15}"
Here's the JSFiddle: http://jsfiddle.net/aScpD/2/
这是JSFiddle:http://jsfiddle.net/aScpD/2/
3 个解决方案
#1
6
Try
\d
--> for numbers
\ d - >代表数字
pattern="\d{15,15}"
Update as commented by s1lence
pattern="\d{15}"
Update to set Custom validation message
Add this attribute to input field
将此属性添加到输入字段
oninvalid="this.setCustomValidity('15 digit number needed')"
Use as commented by Jukka K. Korpela (check above fiddle for working demo)
使用Jukka K. Korpela评论(检查以上小提琴的工作演示)
title="Exactly 15 digits"
#2
0
You can also do it like this.
你也可以这样做。
<input title="requires exactly 15 digits" maxlength="15" pattern=".{15}" required />
Demo
#3
0
Try this:
pattern = /[0-9]{15}/
-
[0-9]
match a number (safer than\d
because\d
matches also foreign number) Edit: According to Bergi, it's not true in JS, but it's still valid in a lot of Perl RegEx implementations.[0-9]匹配一个数字(比\ d更安全,因为\ d匹配外部数字)编辑:根据Bergi,它在JS中不是真的,但它在许多Perl RegEx实现中仍然有效。
-
{15}
is similar to{15, 15}
.{15}类似于{15,15}。
#1
6
Try
\d
--> for numbers
\ d - >代表数字
pattern="\d{15,15}"
Update as commented by s1lence
pattern="\d{15}"
Update to set Custom validation message
Add this attribute to input field
将此属性添加到输入字段
oninvalid="this.setCustomValidity('15 digit number needed')"
Use as commented by Jukka K. Korpela (check above fiddle for working demo)
使用Jukka K. Korpela评论(检查以上小提琴的工作演示)
title="Exactly 15 digits"
#2
0
You can also do it like this.
你也可以这样做。
<input title="requires exactly 15 digits" maxlength="15" pattern=".{15}" required />
Demo
#3
0
Try this:
pattern = /[0-9]{15}/
-
[0-9]
match a number (safer than\d
because\d
matches also foreign number) Edit: According to Bergi, it's not true in JS, but it's still valid in a lot of Perl RegEx implementations.[0-9]匹配一个数字(比\ d更安全,因为\ d匹配外部数字)编辑:根据Bergi,它在JS中不是真的,但它在许多Perl RegEx实现中仍然有效。
-
{15}
is similar to{15, 15}
.{15}类似于{15,15}。