@shagital/adonisjs-phone-validator
v1.0.2
Published
Phone number validator for Adonisjs
Downloads
1
Maintainers
Readme
Adonisjs Phone Validator ▲
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.