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}
/>
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