# Dynamically Format Phone Numbers by Country in the KendoReact MaskedTextBox

## Environment

<table>
    <tbody>
	    <tr>
	    	<td>Product Version</td>
	    	<td>8.2.0</td>
	    </tr>
	    <tr>
	    	<td>Product</td>
	    	<td>Progress® KendoReact MaskedTextBox</td>
	    </tr>
    </tbody>
</table>

## Description

How can I dynamically format a phone number based on a country code selected from a DropDownList using the MaskedTextBox?

## Solution

1. Define a list of countries with their corresponding phone number masks and prefixes:

```jsx
const countries = [
    { name: 'Andorra', code: '🇦🇩', mask: '000-000-000', prefix: '+376' },
    { name: 'Germany', code: '🇩🇪', mask: '0000-0000000', prefix: '+49' }
];
```

2. Handle the [onChange](https://www.telerik.com/kendo-react-ui/components/dropdowns/api/dropdownlistprops#onChange) event of the DropDownList to update the selected country and mask format:

```jsx
const handleCountryChange = (event) => {
    const selected = countries.find((country) => country.code === event.target.value);
    setSelectedCountry(selected);
    setMask(selected ? selected.mask : '');
    setValue('');
    setFormattedValue('');
};
```

3. Handle the [onChange](https://www.telerik.com/kendo-react-ui/components/inputs/api/maskedtextboxprops#onChange) event of the MaskedTextBox to update the input value, apply the mask format and dynamically format the phone number:

```jsx
const handleOnChange = (event) => {
    const newValue = event.target.value;
    setValue(newValue);
    const cleanValue = newValue.replace(/[^0-9]/g, '');
    if (selectedCountry) {
        setFormattedValue(`${selectedCountry.prefix}${cleanValue}`);
    } else {
        setFormattedValue(newValue);
    }
};
```

[Demo Source](https://demos.telerik.com/kendo-react-ui/knowledge-base/maskedtextbox/maskedtextbox-country-formatting/source.txt)
