Phone number validation

Often times you need to validate phone numbers in a particular format that should be used across the website. Rather tha requiring you to use a certain phone number format, Bolt allows you to define your own validation rules for field values. In this example, I'll show you how to require editors to input the desired phone number format.

Usually international phone number formats consist of special characters, such as '+' and ' ' (blank), which makes the field type number unsuitable for storing this kind of information. Instead, the most appropriate field type is text, for example:

            type: text

What we need to do to validate phone numbers is to provide Bolt, in the contenttypes.yaml, with our desired validation rule for the phone field. These validation rules are called regular expressions. For example, here is a regular experssion that validates US phone numbers:


Now, to tell Bolt to use this to validate the phone field when editors input information, we have to update our ContentType definition in contenttypes.yaml:

            type: text
            pattern: '\d{3}[\-]\d{3}[\-]\d{4}'

That's it. Now, when editors fill in the phone field, Bolt will require them to adhere to the correct format in order to save the content. If not, when attempting to save the changes, the field will be highlighted with a notification that the required format is not adhered to.

To read more about the pattern option, please check the official Bolt documentation.

Fields Validation Regex Phone Telephone

Permalink - Written by Ivo on July 1, 2020.

« Search on ContentType listing page - Simple OG-tags for Facebook, Twitter and others »