JavaScript中的电话号码

JavaScript中的电话号码

本文介绍了RegEx for JavaScript中的电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用RegEx验证用户电话号码。我有一套电话号码验证要求。我对RegEX不太了解。任何人都可以帮我提供匹配的reqex我的要求。验证非常简单。

I was using RegEx to validate user phone numbers. I have set of requirements for phone number validation. I dont have much idea about the RegEX. can anyone help me to provide a matching reqex for my requirement. Here the validation is very simple.

条件

 1. It should allow numbers from 0-9.
 2. It should allow + sign. (+ sign is not mandatory but not allow after even a single letter (ie: should be placed at front.)).
 3. It should allow - sign. (- sign is also not mandatory and can be placed anywhere)
 4. It should allow empty space everywhere.
 5. No dots or no other chars allowed except the above said things.

正确值

+65-12345678
65-12345678
6512345678
65 12345678
65 123 45678
65 123-45678
+65 123-45678
1234

InCorrect值

12345+86666
123alpha

谢谢

推荐答案

只是为了帮助你构建一些概念。

以下正则表达式与您提供的前七个输入相匹配。

Just to help you build some concepts.
The following regex would match the first seven inputs you provided.

/^\+?\d{2}[- ]?\d{3}[- ]?\d{5}$/

\ +?将匹配 + 符号。其中的使 + 符号可选。

\\ \\ d {2} 匹配两位数


[ - ]?匹配 - (空格)。 会出现 - (空格)可选。$
\d {5} 然后匹配5位数。

^ $ 是开始和结束锚点。

\+? would match a + sign. The ? in it makes the + sign optional.
\d{2} matches two digits
[- ]? matches either a - or a (space). ? makes the occurrence of - or (space) optional.
\d{5} then matches 5 digits.
^ and $ are the start and end anchors.

这篇关于RegEx for JavaScript中的电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 07:53