JavaScript电话号码验证(拒绝带字母字符的输入)

时间:2012-10-02 00:43:17

标签: javascript jquery forms validation

我正在创建简单的表单验证,排除任何包含字母的条目(基本上来自a-z或A-Z)。

这是我目前使用的,但我的脚本只拒绝任何不是0-9的东西。这将拒绝我想验证的句号,括号和破折号。

var numericReg = /^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$/;
    if(number != "" && !numericReg.test(number)) {
    return false;

我如何只检查信件?

另一种方法是只接受编号为0-9的输入和以下字符:“。”,“ - ”,“(”,“)”

2 个答案:

答案 0 :(得分:5)

两个正则表达式供您尝试。第一个匹配仅包含您指定的字符的字符串。第二个匹配不包含字母a-z或A-Z的字符串:

var isPhoneChar = /^[-.() \d]+$/;
var isNonAlpha = /^[^a-z]+$/i;

请注意,isNonAlpha无法防范ñüṃȅɍǒǘṩǘṩȯẗḥḛṝḽʈťĕřś!

您可以考虑允许x进行电话分机,+进行国际拨号。

答案 1 :(得分:1)

您可以随时使用它直接拉出数字。

CoffeeScript的:

console.log number.match ///

  # Match the start of the string.
  ^

  # Get the first 3 digits.
  \(?(?=\d{3})(\d{3})[).\-\s]*

  # Get the next 3 digits.
  (\d{3})[.\-\s]*

  # Get the last 4 digits
  (\d{4})

  # End of the number
  $

///

Javascript(或节点):

console.log(number.match(/^\(?(?=\d{3})(\d{3})[).\-\s]*(\d{3})[.\-\s]*(\d{4})$/));

以下是我在CoffeeScript中运行的测试用例,以确保它有效:

exports '1234567890'
exports '123 456 7890'

exports '123.456.7890'
exports '123 456.7890'
exports '123.456 7890'
exports '123456.7890'
exports '123.4567890'
exports '123456. 7890'

exports '123-456-7890'
exports '123 456-7890'
exports '123-456 7890'
exports '123456-7890'
exports '123-4567890'

exports '(123)456-7890'
exports '(123)4567890'
exports '(123) 4567890'
exports '(123)456 7890'
exports '(123) 456 7890'
exports '(123) 4567890'

他们的输出:

1234567890
[ '1234567890',
  '123',
  '456',
  '7890',
  index: 0,
  input: '1234567890' ]
123 456 7890
[ '123 456 7890',
  '123',
  '456',
  '7890',
  index: 0,
  input: '123 456 7890' ]
123.456.7890
[ '123.456.7890',
  '123',
  '456',
  '7890',
  index: 0,
  input: '123.456.7890' ]
123 456.7890
[ '123 456.7890',
  '123',
  '456',
  '7890',
  index: 0,
  input: '123 456.7890' ]
123.456 7890
[ '123.456 7890',
  '123',
  '456',
  '7890',
  index: 0,
  input: '123.456 7890' ]
123456.7890
[ '123456.7890',
  '123',
  '456',
  '7890',
  index: 0,
  input: '123456.7890' ]
123.4567890
[ '123.4567890',
  '123',
  '456',
  '7890',
  index: 0,
  input: '123.4567890' ]
123456. 7890
[ '123456. 7890',
  '123',
  '456',
  '7890',
  index: 0,
  input: '123456. 7890' ]
123-456-7890
[ '123-456-7890',
  '123',
  '456',
  '7890',
  index: 0,
  input: '123-456-7890' ]
123 456-7890
[ '123 456-7890',
  '123',
  '456',
  '7890',
  index: 0,
  input: '123 456-7890' ]
123-456 7890
[ '123-456 7890',
  '123',
  '456',
  '7890',
  index: 0,
  input: '123-456 7890' ]
123456-7890
[ '123456-7890',
  '123',
  '456',
  '7890',
  index: 0,
  input: '123456-7890' ]
123-4567890
[ '123-4567890',
  '123',
  '456',
  '7890',
  index: 0,
  input: '123-4567890' ]
(123)456-7890
[ '(123)456-7890',
  '123',
  '456',
  '7890',
  index: 0,
  input: '(123)456-7890' ]
(123)4567890
[ '(123)4567890',
  '123',
  '456',
  '7890',
  index: 0,
  input: '(123)4567890' ]
(123) 4567890
[ '(123) 4567890',
  '123',
  '456',
  '7890',
  index: 0,
  input: '(123) 4567890' ]
(123)456 7890
[ '(123)456 7890',
  '123',
  '456',
  '7890',
  index: 0,
  input: '(123)456 7890' ]
(123) 456 7890
[ '(123) 456 7890',
  '123',
  '456',
  '7890',
  index: 0,
  input: '(123) 456 7890' ]
(123) 4567890
[ '(123) 4567890',
  '123',
  '456',
  '7890',
  index: 0,
  input: '(123) 4567890' ]

如果number.match使用该代码返回null,那么它不是有效数字。这是一种简单的检查方法,然后已经解析了数字。

如果您想要允许a-z,只需将所有\d条目更改为[\d\w]即可。 (See this