Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

147
Visualizações
Correct RegEx for danish mobile number

I have found the following regex ( ^(((?+45)?)?)(\s?\d{2}\s?\d{2}\s?\d{2}\s?\d{2})$ ) and ( /(?:45\s)?(?:\d{2}\s){3}\d{2}/ ) to validate danish mobile number. However it mismatches the following input which are also valid mobile number:

4511760574, 004511760574

A mobile number is only valid if the number is one of the following

a) its either 8 digits b) 8 digits starting with either 45, +45 or 0045 c) First two digits after excluding either 45, +45 or 0045 should be the following as per screenshot (section marked with blue color):

Source: https://en.wikipedia.org/wiki/Telephone_numbers_in_Denmark

PS: I am not 100% confident if (c) is up to date. However a regex would be really appreciated which I can change easily by adapting if any update happens with (c)

Below are the code snippet only related to the question:

****HTML****
<input type="text" class="form-control num-only-with-plus rounded-0" id="phone"
                            placeholder="Telefonnummer" required maxlength="12">
<div id="phone-number-feedback" class="invalid-feedback">Invalid Number</div>

****JS****
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"
    integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ=="
    crossorigin="anonymous"></script>

<script type="text/javascript">

function validateMobileNumber(phone) {
        const pattern = /(?:45\s)?(?:\d{2}\s){3}\d{2}/g;
        return pattern.test(String(phone).toLowerCase());
    }

//Allow only digit and '+' sign on the input field
$('.num-only-with-plus').keypress(function (e) {
    if (e.key != '+' && e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
            return false;
        }
$('#phone').keyup(_.debounce(function () {
            const phone = $(this)
            if (validateMobileNumber(phone.val())) {
                phone.toggleValid()
                $('#submit_btn').prop('disabled', false)                
            } else {
                phone.toggleInvalid();
                $('#phone-number-feedback').text("Tjek venligst mobilnummeret igen. Er det okay?")
                $('#submit_btn').prop('disabled', true)
            }
        }, 500));
</script>


Please help!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Let's go through the requirements to build up a pattern: first and second ones...

a) its either 8 digits

b) OR 8 digits starting with either 45, +45 or 0045

... are rather direct, and can be expressed like this:

(?:(?:00|\+)?45)?\d{8}

The heart of this expression is \d{8} part (meaning 8 digits). What goes before it is 'double-optional' part, meaning roughly '...optionally preceded by 45, which itself can be optionally preceded by either 00 or + sign'. ?: sigils are used to prevent regex engine to capture the content of those character groups, making a pattern a little bit faster and less memory-consuming.

So far, so good, but how can we express that third requirement, meaning there's a specific set of digits at the beginning of \d{8} part? While it's possible to modify the 'heart part' to include those clauses, there's a more maintainable alternative - positive lookaheads:

(?=2|3[01]|4[012]|4911|5[0-3]|6[01]|[78]1|9[123])

This part of a pattern is only satisfied if a specific position in the checked string is followed either by 2, or 30-31, or 40-42, or 4911, or 50-53, or 60-61, or 71, or 81, or 91-93 - just as written in that wiki page. If anything changes, it'll be easy to update that part without touching any other part of regex.

So the final pattern looks something like this:

^(?:(?:00|\+)?45)?(?=2|3[01]|4[012]|4911|5[0-3]|6[01]|[78]1|9[123])\d{8}$

And here's regex101 demo, which you can use to play with both pattern and sample strings.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda