Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!