jquery validation regex用于带连字符的数字

ymdaylpp  于 9个月前  发布在  jQuery
关注(0)|答案(8)|浏览(59)

有人能提供一个正则表达式,只允许数字和可选连字符
我已经得到了电话号码.匹配(/[0-9-]/);但运气不好

ncecgwcz

ncecgwcz1#

以下匹配的数字中穿插着破折号,但不是双破折号:

/^\d+(-\d+)*$/
ar5n3qh5

ar5n3qh52#

你所描述的是在jquery验证插件http://docs.jquery.com/Plugins/Validation/CustomMethods/phoneUS中使用的

^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$

我来到这里寻找类似的东西,并发现我需要在http://regexlib.com/他们有一个伟大的表达式集合,以及一个有用的小抄。

bbmckpt7

bbmckpt73#

试试这个plugin:屏蔽输入jQuery插件。它允许制作自己的面具并简化用户的输入。

vsikbqxv

vsikbqxv4#

/^\w*-?\w*-?\w*$/

**\w 字母数字字符重复0次或多次。
***-?**可选连字符。

wwtsj6pe

wwtsj6pe5#

试试这个...

/^[0-9]+(\-[0-9]+)*$/

不是电话号码,而是你想从评论中得到什么

qoefvg9y

qoefvg9y6#

Check this out...

var phoneno = /^+?[0-9]+(-[0-9]+)*$/;
电话:+954-555-1234

kuhbmx9i

kuhbmx9i7#

下面是带连字符的输入的解决方案

$('input[type="tel"]').keyup(function() {
  this.value = this.value
    .match(/\d*/g).join('')
    .match(/(\d{0,3})(\d{0,3})(\d{0,4})/).slice(1).join('-')
    .replace(/-*$/g, '');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="tel"></input>
8ehkhllq

8ehkhllq8#

我最近为我的作业开发了一个正则表达式模式,它完全匹配999-999-9999。
/^{3}-{1}\d{3}-{1}\d{4}$/

^ asserts position at start of a line
\d matches a digit (equivalent to [0-9])
{3} matches the previous token exactly 3 times
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
{1} matches the previous token exactly one time (meaningless quantifier)
\d matches a digit (equivalent to [0-9])
{3} matches the previous token exactly 3 times
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
{1} matches the previous token exactly one time (meaningless quantifier)
\d matches a digit (equivalent to [0-9])
{4} matches the previous token exactly 4 times
$ asserts position at the end of a line

相关问题