Regular expression for Dutch phone number
Asked Answered
C

9

7

I'm looking for a regular expression to match a Dutch phone number. These are the formatting requirements:

  • Should start with 0
  • Contains maximum of 1 (optional) dash "-" character, for now it does not matter where it is, as long as it's not the first character
  • Total length 10 or 11 characters

This is what I've come up with so far:

^0+-{1}?([0-9]{10,11})$
Clayborn answered 30/7, 2013 at 14:25 Comment(5)
oh please what in the world is this -{1}? ? Just use -? to make the dash optionalNorite
@Norite it should have been {0,1} if they're recreating the functionality of the ?.Paternal
@AbsoluteƵERØ Even so, why use {0,1} instead of ? :) ps: I've heard some people like that syntax :oNorite
@Floran, the dash character can't be the first character or the first character before the 0?Buffon
What language are you using?Dorian
T
41

I have seen this before at http://regexlib.com/Search.aspx?k=phone%20number Check this website out, hope it helps.

(^\+[0-9]{2}|^\+[0-9]{2}\(0\)|^\(\+[0-9]{2}\)\(0\)|^00[0-9]{2}|^0)([0-9]{9}$|[0-9\-\s]{10}$)

Regular expression to evaluate dutch-style phone numbers. Possible example prefixes: +31, +31(0), (+31)(0), 0, 0031 followed by 9 numbers (which can contain a space or -).

Taipan answered 30/7, 2013 at 14:32 Comment(6)
This should be a comment, so gain 50 rep quickly !Norite
Is this regex valid for Dutch mobile phone numbers also?Coven
Somehow it allows for 11 digits phone numbers. As i recall all Dutch phone numbers are 10 digits.Hoopen
I would suggest changing the last 10 to a 9.Hoopen
By changing the last 10 to a 9 you will loose support for a space in your phonenumberWun
When you trim the phone number first you can wildly simplify this to ^(?:\+31|0031|0)[1-9][0-9]{8}$, Dutch phone numbers always start with a 0, which you is removed when you add the country code, followed by a non zero number and then 8 more 0-9 numbers.Curley
P
11

I came up with this creation

^(?:0|(?:\+|00) ?31 ?)(?:(?:[1-9] ?(?:[0-9] ?){8})|(?:6 ?-? ?[1-9] ?(?:[0-9] ?){7})|(?:[1,2,3,4,5,7,8,9]\d ?-? ?[1-9] ?(?:[0-9] ?){6})|(?:[1,2,3,4,5,7,8,9]\d{2} ?-? ?[1-9] ?(?:[0-9] ?){5}))$

Piggish answered 6/11, 2013 at 14:20 Comment(3)
Why the downvotes? It still is the most complete answer. not that readable, but complete for all house numbers and mobile numbers. Spaces may occur everywhere, dashes only after the regional part, after the dash only 1-9, in case of 3 of 4 numbers in the regional part, no starting 6 is allowed. or 0.Terse
I think [1,2,3,4,5,7,8,9] should be [12345789] or [1-57-9].Zoellick
It makes it a little more readable. One extra thing to make it better, is to first remove all spaces from the string. Then your only worry is the correct place of the dash. So starting with either 0 or 0031 or +31 Followed by one of these ``` 6-[1-57-9][0-9]{7} (mobile numbers 06, then 8 nr.) or [1-57-9][0-9]-[1-9][0-9]{6} (eg 023, 010, + 7 nr.) or [1-57-9][0-9]{2}-[1-9][0-9]{5} (eg 0598 + 6 nr.) ```Terse
L
6

Try this regex: ^(?=^.{10,11}$)0\d*-?\d*$

Lulu answered 30/7, 2013 at 14:35 Comment(1)
I like it, but I think you need a $ in your lookahead cause otherwise longer strings will pass.Dorian
H
5

Here is a regex for Dutch mobile phonenumbers including a check for international prefixes (0031 or +31). Check it at: https://regex101.com/r/Y2DG19/1

^\(?([+]31|0031|0)-?6(\s?|-)([0-9]\s{0,3}){8}$

Hope this helps people out.

Homocyclic answered 21/11, 2017 at 16:16 Comment(0)
H
0

here's my solution:

extension String {
    func isValidPhone() -> Bool {
        let dutchRegex = "^(06[0-9]{8}|[+]{1}31[0]?[0-9]{9,10}|0031[0]?[0-9]{9,10})"
        let phonePredicate = NSPredicate(format:"SELF MATCHES %@", dutchRegex)
        return phonePredicate(with: self) 
    }
}

This will validate all numbers with +31, 06, 0031 so will only work for Dutch phone numbers

Haggle answered 5/3, 2019 at 14:28 Comment(0)
H
0

You can also do it like this. First you remove all not digits and then you check the number with the regex.

const regex = /^((\+|00)?31|0(?!0))(\d{9})$/;
const valid = regex.test(input.replace(/\D/g, ''));

It works with numbers starting with 0031, +31, 0.

Haruspicy answered 3/4, 2019 at 14:20 Comment(0)
B
0

I tried to elaborate on @Raj's solution by adding some more constraints on the use of hyphens or spaces, because it somehow captured 06-01-2019 as well. I placed some comments in there as well to provide examples:

\b(\+[0-9]{2}(?# e.g. +31)|^\+[0-9]{2}\(0\)|\(\+[0-9]{2}\)\(0\)(?# e.g. +31(0)|00[0-9]{2}(?# e.g. 0031)|0)([6][\-\s]?[1-9][0-9]{7}(?# e.g. 0612345678 or 06-12345678 or 06 12345678)|[6][\-\s]?[1-9][0-9]\s?[0-9]{2}\s?[0-9]{2}\s?[0-9]{2}(?# e.g. 06-12 34 56 78)|[6][\-\s]?[1-9][0-9]\s?[0-9]{3}\s?[0-9]{3}(?# e.g. 06-12 345 678)|[6][\-\s]?[1-9][0-9]{2}\s?[0-9]{3}\s?[0-9]{2}(?# e.g. 06-123 456 78)|[0-9]{2}[\-\s]?[1-9][0-9]{6}(?# e.g. 030-1234567)|[0-9]{2}[\-\s]?[1-9][0-9]{2}\s?[0-9]{2}\s?[0-9]{2}(?# e.g. 030-123 45 67)|[0-9]{2}[\-\s]?[1-9][0-9]\s?[0-9]{2}\s?[0-9]{3}(?# e.g. 030-12 34 567))\b

(I know... And I didn't even cover all cases containing whitespaces)

Brittni answered 28/6, 2019 at 13:13 Comment(0)
T
0
^(((\\+31|0|0031)6){1}[1-9]{1}[0-9]{7})$

Short and sweet. A slight improvement on Peter's answer. (The first number after the 6 can not be a 0.)

Tubular answered 21/12, 2023 at 10:5 Comment(0)
C
-1

I have generated the following one for Dutch mobile numbers. Until now, I tested with some possibilities and was able to cover them. I hope it helps.

\(?(31\s*|0)-?6(\s*\)?|-?\s*)([0-9]\s{0,3}){8}
Casias answered 18/9, 2017 at 13:31 Comment(3)
*)?| is saying that an ) is allowed, so this is not right.Homocyclic
In my case, a user can type with parentheses, so I need to handle them. For instance (06)12345678 should be handled.Casias
My case was not validation, instead, to look for a possible dutch number @PknifeCasias

© 2022 - 2025 — McMap. All rights reserved.