I need a code to add a feature to my funnels. I would like to set a prefix to the phone number field. For example “+57” in order to the customer don’t write the country code. This prefix can not to be delete by the customer.
An additional wanted feature is the phone number field only accept numeric characters and having a fixed long.
1 Like
Hello @idelasotta ,
First of all, extremely sorry for the long delay on this, this thread was buried under our recent posts. and thank you for reminding us through the direct chat.
Here is the code you can use to enforce a phone number prefix:
<script>
const PHONE_PREFIX = "+56";
document.querySelectorAll('input[name="phone"]').forEach(i=>{
if (!i.value) i.value = PHONE_PREFIX;
if (!i.value.startsWith(PHONE_PREFIX)) {
i.value = PHONE_PREFIX + i.value.replace('+','');
}
i.dataset.oldPhone=i.value;
i.addEventListener('input', (e)=>{
if (!i.value.startsWith(PHONE_PREFIX) && i.dataset.oldPhone && i.dataset.oldPhone.startsWith(PHONE_PREFIX)) {
i.value = i.dataset.oldPhone;
}
i.dataset.oldPhone = i.value;
})
})
</script>
How it works?
-
Copy the code above as it is into your page Custom Codes > Body JS.
-
You can modify the phone prefix on the first line if needed ‘
PHONE_PREFIX
’"+56"
-
Save changes and take it for a spin, do let us know if it needs any improvement.