Manoj Sethi
Manoj Sethi

Reputation: 2038

Sweden Phone Number Regular Expression

Hello I am using following regular expression for validating Sweden number

^(([+]46)((70[{0-9}])|(72[{0-9})])|(73[{0-9}])|(76[{0-9}]))([\d]{6}))$

I want my number to be in following format +46 70 5689 123 or +46705689123

Please help me in doing this.

Upvotes: 4

Views: 17954

Answers (7)

Janspeed
Janspeed

Reputation: 2814

I'll write this since the topic says "swedish phone number" and not "swedish mobile phone number". Despite it might not be exactly what the QS asked for.

Swedish land line numbers may have any number after the prefix, a lot of different whitspace setups depending on the length of the numbers and sometimes they are also written with hyphens.

Length 0+9 numbers
08-xxx xxx xx
0xx-xxx xx xx
0xxx-xx xx xx

Length 0+8 numbers
08-xxx xx xx
0xx-xx xx xx
0xxx-xxx xx

Length 0+7 numbers
08-xx xx xx
0xx-xxx xx

Length 0+9 numbers (Mobile)
07x-xxx xx xx

The prefix +46 or 00 could also be applied to those numbers. Also adding the notation the QS is asking for:

+46 70 5689 123
+46705689123
+46 85 68 91 23

This makes keeping track of all those whitespaces quite bothersome when writing a regexp. Thats why my suggestion would be to trim/remove all the spaces and/or hyphens and then run this regexp instead:

^(([+]46)|(0)|(00))(\d{7,9})$

In my opinion you will be better off by standardizing how you enter numbers in your database and not adding unneccesary whitespaces to avoid polluting the data anyways.

Sources:

https://sv.wikipedia.org/wiki/Telefonnummer

https://sv.wikipedia.org/wiki/Lista_%C3%B6ver_svenska_riktnummer

Upvotes: 0

Niklas Carlefalk
Niklas Carlefalk

Reputation: 11

I use something like this. Can probably be done neater, but it can handle that:

  • Numbers can start with either +46 or 07 or 01.
  • Might include a - like 0736-XXXXXX or 070-XXXXXXX
  • White spaces can also occur anywhere (at least in my case, since we haven't really streamlined the inputs in the past).

^((([+]46)\s*((1|7)[0236]))|(0(1|7)[0236]))\s*(([-]|())\s*[0-9]\s*[0-9]\s*[0-9]\s*[0-9]\s*[0-9]\s*[0-9]\s*[0-9]\s*|([0-9]\s*([-]|()))\s*[0-9]\s*[0-9]\s*[0-9]\s*[0-9]\s*[0-9]\s*[0-9]\s*)$

Upvotes: 1

Kashif Saeed
Kashif Saeed

Reputation: 147

/^07(0|2|3|6|9)\d{7}$/g

this will work for the local number starts with following initials: 070, 072, 073, 076, 079 Mobile phone networks example: 076xxxxxxx x will be the numeric

Upvotes: 0

oygen
oygen

Reputation: 507

^((((0{2}?)|(\+){1})46)|0)7[\d]{8}

will work with 0046 708 777 666

try it out

Upvotes: 0

Per
Per

Reputation: 71

For the sake of having a complete answer I'll add in to this thread. Manoj, you wrote in the comment that you also want to allow numbers starting with 0 which the answer from Stephan (albeit great) doesn't cover.

You want to use

^(([+]46)\s*(7)|07)[02369]\s*(\d{4})\s*(\d{3})$

To allow:

  • +46 7
  • +467
  • 07

Followed by 0/2/3/6/9, (a possible space), 4 digits, (a possible space), 3 digits

Regular expression visualization

Debuggex Demo

Upvotes: 3

Stephan
Stephan

Reputation: 43023

The spaces (\s*) are missing in the regex:

^([+]46)\s*(7[0236])\s*(\d{4})\s*(\d{3})$

Description

Regular expression visualization

Demo

https://www.debuggex.com/r/vG3FFsIeqa82QQRS

Discussion

I have replaced the parts like this one 70[{0-9}] with something more readable: 7[0236].

Upvotes: 15

Jonny 5
Jonny 5

Reputation: 12389

Why so many brackets?

^\+46 *7[0236] *\d{4} *\d{3}$

should do it.

Upvotes: 4

Related Questions