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

112
Visualizações
React: Always Keep the first four characters onchange

So basically I have an input component for phone numbers. I am using the react native phone input library, so the international code is added by default. I want to set the input change in such a way that the international code +234 never gets deleted with the back button. I believe regex is the way to go using let regex = /[^+234]/g to match the start but I don't know how to proceed next.

So basically if a user types any number in, +234 is added by default to the beginning of phoneNumber. With the current design of the library, the +234 prefix gets deleted when the user deletes the number . What I want is for the +234 to be kept no matter how much the user deletes

CODE

 const onChangePhoneNumberHandler = (phoneNumber: string) => {
        setPhoneNumber(phoneNumber)
        showMessage({ text: ``});
        
    }

    <PhoneInput 
         value={phoneNumber}
        onChangePhoneNumber={onChangePhoneNumberHandler}
    />
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

const onChangePhoneNumberHandler = (phoneNumber?: string) => {
    const newNumber = phoneNumber?.includes("+234") ? phoneNumber : "+234"
    setPhoneNumber(newNumber)
    showMessage({ text: ``});
    
}

Might wanna fine tune the logic part but that is a quick solutin

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