0

I want to validate phone number with javascript, so far i have regex that looks like this:

/^[0-9\+]{8,13}$/

But it is not what i finally wanna get. I need get phone number format that is default for my country "Poland". For example few phones formats i need check with regex if user correctly passed them: 321123321, 123-321-123, 123 321 123, 123211212, 12-321-12-12, 12 321 12 12.

Sorry if my question is silly but i have no idea how to understand this regex.

kuchar
  • 607
  • 1
  • 8
  • 23

2 Answers2

3

Looks like you are allowing digits spaces and hypens.

Why don't you remove spaces and hyphens and check if rest of them are digits or not.

var str = "123 321 123";
str = str.replace( /\s|-/g, "" );

Now run your own regex on it

"123 321 123".replace( /\s|-/g, "" ).match(/^[0-9\+]{8,13}$/)

Or simply include space and hyphen in the regex

"123 321 123".match(/^[0-9\+\s-]{8,13}$/);
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

Use below regex which will allow - and blank space and all the characters should be between 8 and 13.

^[0-9\s- \+]{8,13}$
Rahul Patel
  • 5,248
  • 2
  • 14
  • 26