### [How to Restrict the Phone Field to Only Accept Numbers](https://wpforms.com/developers/how-to-restrict-phone-field-to-only-accept-numbers/)

**Published:** August 12, 2021
**Author:** Editorial Team

**Excerpt:** In this tutorial, we will show you how to restrict the Phone field to only accept numbers using a small script. 

**Content:**

Would you like to restrict the **Phone** field to only accept numbers? By default, the **International** and **Smart** phone formats will allow you to enter letters, but will fail on validation when you tab out of the field.

![default phone validation error will show when you tab out of the field](https://wpforms.com/wp-content/uploads/2021/08/wpforms-default-phone-validation.jpg)

Using a small code snippet you can easily disable any keyboard characters from being entered unless it’s a number. This means, that if they attempted to type in anything but numbers, the text would simply not be entered, thus, disabling the field until a number was entered.

## Creating the form

First, you’ll need to create a new form and add your fields. If you need any assistance in creating your form, [please see this documentation](https://wpforms.com/docs/creating-first-form/ "How to Create Your First Form").

![create your form and add your fields](https://wpforms.com/wp-content/uploads/2021/08/wpforms-create-your-form.jpg)

## Adding the CSS class

Next, you’ll need to add a CSS class to the **Phone** form field. Whenever a **Phone** field with this class is used, it will **only** accept a numeric value.

By adding this CSS class to any form field that accepts the CSS classes, you can restrict that field to accept only numbers.

To add a CSS class, select the Phone form field you’ve added to your form and in the **Advanced** tab, add **numbersOnly** to the **CSS Classes**.

![add the CSS class of numbersOnly to the Phone field](https://wpforms.com/wp-content/uploads/2021/08/wpforms-add-css-class.jpg)

## Restricting the phone field to numbers only

Now it’s time to add the snippet to your site that would restrict the field only to allow numbers.

If you need any help in adding snippets to your site, [please review this tutorial](https://wpforms.com/developers/how-to-add-custom-php-or-javascript-for-wpforms/ "How to Add Custom PHP or JavaScript for WPForms").

```

/**
 * Restrict the Phone Field to Only Accept Numbers.
 *
 * @link https://wpforms.com/developers/how-to-restrict-phone-field-to-only-accept-numbers/
 */

function restrict_letters_in_phone_field( ) {
?>

**Categories:** Fields

**Tags:** Javascript

---

