最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regex for Brazilian phone number - Stack Overflow

programmeradmin3浏览0评论

I am attempting to setup a regex verification for a user to enter a Brazilian telephone number. The formats I would need it to accept (or as close as possible) would be: 22-22222-2222 22222222222 (22) 22222 2222 (22)-22222-2222 22 22222 2222 (22)222222222

So it needs to accept numbers, spaces, dashes, parentheses only. Any advice on this would be greatly appreciated!

I am attempting to setup a regex verification for a user to enter a Brazilian telephone number. The formats I would need it to accept (or as close as possible) would be: 22-22222-2222 22222222222 (22) 22222 2222 (22)-22222-2222 22 22222 2222 (22)222222222

So it needs to accept numbers, spaces, dashes, parentheses only. Any advice on this would be greatly appreciated!

Share Improve this question asked Oct 18, 2018 at 21:11 J33NOJ33NO 9391 gold badge8 silver badges14 bronze badges 3
  • 1 what about + at start? And what is your code? How are you tried to accomplish this? – BladeMight Commented Oct 18, 2018 at 22:53
  • No I dont think we will need the + at the start. I currently have javascript verifying the input to meet certain validations. If the validation isnt met, the textbox goes red and the "Submit" button is greyed out. As it stands it has a regex that only allows for numeric values but the problem is when people try to add their own symbols, dashes, parenthesis the javascript validation keeps the box red and this confuses people apparently. – J33NO Commented Oct 18, 2018 at 23:12
  • Then you should change your regex to accept not only numeric values, try this one: \(?\d{2,}\)?[ -]?\d{4,}[\-\s]?\d{4}, also check that your javascript constantly checks the format on some event, e.g. on the <input onchange="yourcheck_function()"> or input.oninput = function(){} – BladeMight Commented Oct 18, 2018 at 23:17
Add a comment  | 

7 Answers 7

Reset to default 8

I just made a currently valid regex for Brazilian numbers:

^\s*(\d{2}|\d{0})[-. ]?(\d{5}|\d{4})[-. ]?(\d{4})[-. ]?\s*$

Check out the validation at https://regex101.com/r/safMl7/2

Will not get the country code, since we know it is from Brazil: +55.

Will work with the following examples, with or without spaces and dashes:

  • 12 1234 1234
  • 12 12345 1234
  • 12345 1234
  • 1234 1234

Each group of the regex will be divided by the correct brazilian grouping.

See this example: 62 98345 1234

Mask for brasil Phone using javascript

regex for format 11 digits: /(\d{2})(\d{1})(\d{4})(\d{4})/, "($1) $2 $3-$4

before no regex : 63991017575
After aply regex: (63) 9 9101-7575

EXAMPLE 1 USING THE JAVASCRIPT:

var number = '63992017738';
number = number.toString().replace(/(\d{2})(\d{1})(\d{4})(\d{4})/, "($1) $2 $3-$4")

Result: (63) 9 9201-7738


EXAMPLE 2 USING THE JAVASCRIPT:

var number = '63992017738';
number = number.toString().replace(/(\d{2})(\d{1})(\d{4})(\d{4})/, "$1 $2 $3-$4")

Result: 63 9 9101-7575


EXAMPLE 3 USING THE JAVASCRIPT:

now + country code
//if you need this: +55 (63) 9 9201-7131
var number = '5563992017131';
number = number.toString().replace(/(\d{2})(\d{2})(\d{1})(\d{4})(\d{4})/, "+$1 ($2) $3 $4-$5")

Result: +55 (63) 9 9201-7131


If you always have the area code (DDD), try a simplified version of Frederiko's regular expression.

^(\d{2})\D*(\d{5}|\d{4})\D*(\d{4})$

Check out at https://regex101.com/r/M1DrBo.

What about this: \(?\d{2,}\)?[ -]?\d{4,}[\-\s]?\d{4}

My answer came in the form of using the Jquery mask plugin. Was exactly what I needed and has much more functionality.

https://igorescobar.github.io/jQuery-Mask-Plugin/

$(document).ready(function () { $('.input-telephone').mask('(99) 9999-9999?9'); });

The following use of Regex seems to catch around 20 of the most common ways to write a Brazilian phone number. Fix and mobile numbers included.

(\b\(\d{2}\)\s?[9]?\s?\d{4}(\-|\s)?\d\d{4})|(\b\d{2}\s?[9]?\s?\d{4}(\-|\s)?\d{4})|(\b([9]|[9]\s)?\d{4}(\-|\s)?\d{4})|(\b\d{4}(\-|\s)?\d{4})

There are four "or" options to match a number. It goes from the more complex one (with parenthesis and DDD code) to the most simple one (i. e. eight digit phone number).

I added the \b in front and not in the end of each case because sometimes you can catch cases where people write a number followed by some description: "99000-1100word". You can remove the word boundaries as you want, though:

(\(\d{2}\)\s?[9]?\s?\d{4}(\-|\s)?\d\d{4})|(\d{2}\s?[9]?\s?\d{4}(\-|\s)?\d{4})|(([9]|[9]\s)?\d{4}(\-|\s)?\d{4})|(\d{4}(\-|\s)?\d{4})

This is the simplier

regex for 10 digits format: (\d{2})(\d{4})(\d{4})

regex for 11 digits format: (\d{2})(\d{5})(\d{4})

Therefore, you could write a function to regex and replace:

export function phoneFormatter10(phone) {
    phone = phone.replace(/[^\d]/g, ""); //remove all non digits
    return phone.replace(/(\d{2})(\d{4})(\d{4})/, "($1)$2-$3");

}

export function phoneFormatter11(phone) {
    phone = phone.replace(/[^\d]/g, ""); //remove all non digits
    return phone.replace(/(\d{2})(\d{5})(\d{4})/, "($1)$2-$3");
}

Examples of use:

phoneFormatter10('3499883424')
result: '(34)9988-3424'
phoneFormatter11('3499^883*4-244') //even with badly formatted string
result: '(34)99883-4244'
发布评论

评论列表(0)

  1. 暂无评论