How to Allow Only Numbers in the Phone Number Field of a Web Widget
If you want the Phone Number field in a BoldDesk Web Widget to accept only numeric input, you can use the Custom JS option in the widget’s Advanced Customization settings.
This customization helps restrict the phone number field so users can enter only numbers and the + symbol while typing.
Custom JS script
Use the following script to allow only numbers and the + symbol in the Phone Number field:
inputField = document.querySelector('.phone-number-field');
inputField.addEventListener('keypress', function(event) {
const regex = /[\d\+]/;
if (!regex.test(event.key)) {
event.preventDefault();
}
});
What this script does
This script targets the Phone Number field by using the .phone-number-field selector.
It then checks each typed character and allows only:
- numbers (
0–9) - the plus sign (
+)
If a user types any other character, the script blocks it.
This regex allows only numbers and the + symbol. If you want to allow a different phone number format, update the regex based on your requirement.
Add the custom JS to the web widget
To apply this script in BoldDesk, follow these steps:
-
Go to Admin.
-
Make sure the Help Desk tab is selected.
-
In the left pane, select Web Widgets & AI Agent.
-
Open the widget that you want to customize.
-
Scroll to the Advanced Customization section.
-
In the Custom JS box, paste the script.
-
Select Update.
Frequently Asked Questions
-
Can I allow only numbers in the Phone Number field of a BoldDesk Web Widget?
Yes. You can use the Custom JS option in the widget’s Advanced Customization settings to restrict the Phone Number field input. -
Can I change the allowed characters in the Phone Number field?
Yes. You can modify the regex in the custom JavaScript if you want to allow a different phone number format.