簡體   English   中英

RegEx for JavaScript中的電話號碼

[英]RegEx for Phone number in JavaScript

我使用RegEx來驗證用戶電話號碼。 我有一套電話號碼驗證要求。 我對RegEX不太了解。 任何人都可以幫我提供匹配的reqex我的要求。 這里的驗證非常簡單。

條件

 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

謝謝

根據您的樣品,試試這個:

^(?:\+?\d{2}[ -]?\d{3}[ -]?\d{5}|\d{4})$

它將匹配所有正確的值。

描述:

正則表達式可視化

DEMO:

http://regexr.com?340nf

只是為了幫助你構建一些概念。
以下正則表達式將匹配您提供的前七個輸入。

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

\\+? 會匹配一個+號。 ? 在它中使+符號可選。
\\d{2}匹配兩位數
[- ]? 匹配a -或a (空間)。 ? 發生- (空格)可選。
\\d{5}然后匹配5位數。
^$是開始和結束錨點。

根據你的樣品嘗試這種模式。

^(?:\+?\d{2}[ -]?[\d -][\d -]+)$

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM