@shagital/adonisjs-phone-validator

1.0.2 • Public • Published

Adonisjs Phone Validator ▲

npm npm (scoped) NPM

Introduction

Adds phone number validation functionality to Adonisjs on the JS port of Google's libphonenumber API by catamphetamine

Step One - Install

Via Adonis CLI

adonis install @shagital/adonisjs-phone-validator

Via npm/yarn

  • Install package
// via npm
npm require @shagital/adonisjs-phone-validator

// via yarn
yarn add @shagital/adonisjs-phone-validator

Step Two - Register Provider

Open start/app.js and add @shagital/adonisjs-phone-validator/providers/PhoneValidatorProvider to the providers array

Usage

You use the phone validation syntax just like you'd normally do validation in Adonisjs. Examples below: NOTE: The country 2-character ISO code is required. You can find all supported codes here

controller method

//app/Controllers/Http/UserController

const { validate } = use('Validator')

class UserController {

  async store ({ request, session, response }) {
    const rules = {
      phone: 'required|phone:NG', // validate Nigerian phone number
    }

    const validation = await validate(request.all(), rules)

    if (validation.fails()) {
      session
        .withErrors(validation.messages())
     
      return response.redirect('back')
    }

    return 'Validation passed'
  }
}

Validator

//app/Validators/StoreUser.js

'use strict'

class StoreUser {
  get rules () {
    return {
      phone: 'required|phone:US', // validate a US phone number
    }
  }
}

Contribution

Free for all, if you find an issue with the package or think of an improvement, please send in a PR.

Package Sidebar

Install

npm i @shagital/adonisjs-phone-validator

Weekly Downloads

1

Version

1.0.2

License

MIT

Unpacked Size

4.99 kB

Total Files

6

Last publish

Collaborators

  • djunehor