Strapi plugin logo for Phone Validator

Phone Validator

Strapi Phone Validator is a powerful and easy-to-use plugin designed to validate phone numbers in various formats. With support for international phone numbers, it offers comprehensive validation to ensure the accuracy of user-provided phone data in your applications.

Strapi-Phone-Validator

Strapi Phone Validator

Strapi Phone Validator is a powerful and easy-to-use plugin designed to validate phone numbers in various formats. With support for international phone numbers, it offers comprehensive validation to ensure the accuracy of user-provided phone data in your applications.

It's a integration of React International Phone library.

❗ Requirements

  • Strapi v4

🔧 Installation

You just need to install the strapi-phone-validator package via npm or yarn, at the root of your strapi project.

npm:

npm i strapi-phone-validator

yarn:

yarn add strapi-phone-validator

✨ Usage

Create a custom field for a phone number on content type builder page.

Preview

Now you can use Strapi Phone Validator as a custom field.

Preview

You can also validate a phone number also on creating a new entity via API endpoint:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<code>
import { factories, Strapi } from "@strapi/strapi";
import { PhoneNumberUtil } from 'google-libphonenumber';

const phoneUtil = PhoneNumberUtil.getInstance();

const isPhoneValid = (phone: string) => {
  try {
    return phoneUtil.isValidNumber(phoneUtil.parseAndKeepRawInput(phone));
  } catch (error) {
    return false;
  }
};

export default factories.createCoreController(
  "api::.......",
  ({ strapi }: { strapi: Strapi }) => ({
    async create(ctx) {
      const { data } = ctx.request.body;

     const isValid = isPhoneValid(data.phone);


     if(isValid) {
        //...
     }

    ...
    },
  })
);
<code>

To make Strapi Phone Validator work, you should take a look at the next section.

After restarting your Strapi app, Strapi Phone Validator should be listed as one of your plugins.

🚀 Strapi Configuration (required)

Allow Strapi Phone Validator assets to be loaded correctly by customizing the strapi::security middleware inside ./config/middlewares.js.

Instead of:

1
2
3
4
5
export default [
  // ...
  'strapi::security',
  // ...
];

Write:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
export default [
  // ...
    {
        name: "strapi::security",
        config: {
        contentSecurityPolicy: {
            useDefaults: true,
            directives: {
            "connect-src": ["'self'", "https:"],
            "script-src": ["https://cdnjs.cloudflare.com"],
            "media-src": ["https://cdnjs.cloudflare.com"],
            "img-src": ["https://cdnjs.cloudflare.com"],
            },
        },
        },
    },
  // ...
];

Install now

npm install strapi-phone-validator

STATS

9 GitHub stars145 weekly downloads

Last updated

177 days ago

Strapi Version

4.0.0 and above

Author

github profile image for Hadjiu Sorin
Hadjiu Sorin

Useful links

Create your own plugin

Check out the available plugin resources that will help you to develop your plugin or provider and get it listed on the marketplace.