1.2.0 • Published 2 years ago

@runninghill/ng-rsa-form-validators v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Runninghill Logo

Angular South African Form Validators

Please note that this is the README for consumers, if you plan on contributing to this package, please take a look here.

A set of South African specific validators for Angular forms.

Getting Started🏁

If you plan on contributing to the package please read the contributor readme.

  1. Run npm install rh-angular-rsa-form-validators in your Angular project.

  2. Import and use the exposed classes to validate your Angular form controls.

Usage💡

To begin validating your South African specific form fields, start by importing the RsaFormValidators class.

import { RsaFormValidator } from 'rh-angular-rsa-form-validators'

Add the appropriate validator to your form control(s).

// When creating your form
this.form = this.formBuilder.group({
    name: ['', Validators.required],
    mobileNumber: ['', [Validators.required, RsaFormValidator.phoneNumber(false, true)]],
    workNumber: ['']
})

// After the form is already created
this.form.controls['workNumber'].setValidators([RsaFormValidators.phoneNumber(false, true)])

To determine if the form control passed the validation, check the errors object on the form control.

RsaFormValidator error objects on form controls will always be the name of the validator, prefixed with 'rsa' eg; for the rsa phone number validator, the error object is rsaPhoneNumber (rsa + phoneNumber = rsaPhoneNumber)

if (this.form.controls['mobileNumber'].errors.rsaPhoneNumber)
    errorMessage = 'Mobile number must be a valid South African phone number.'

API Reference📖

RsaFormValidator

Contains form control validators used to validate South African specific form fields.

source code

Methods

phoneNumber(allowCountryCode, allowSpaces)

Returns a validator which determines if a form control is a valid South African phone number.

Parameters

NameTypeDescriptionDefault
allowCountryCodebooleandetermines if the phone number is allowed to contain the South African country code (+27)true
allowSpacesbooleandetermines if a phone number is allowed to contain spacestrue