Angular Register Validation Rules
In some cases the rules supplied by the library are not enough for all the application use cases. That's why simple-body-validator has a very simple way to register custom validation rules in Angular.
In the example below we will add a new rule called telephone
, which enforces a specific telephone format.
import { register } from 'simple-body-validator';
register('telephone', function (value) {
return /^\d{3}-\d{3}-\d{4}$/.test(value);
});
tip
It is better to register the rules when the application is first bootstrapped to guarantee that the new rule will be
available at the time of usage. This is why in our example we registered the rule in the main.ts
file.
And that's it, we registered a new custom rule that can be used in the form validation. The only missing part is
to specify the error message linked to the new telephone
rule. In the example below we will
add the telephone
related error message to the en.ts
file.
telephone: 'The :attribute phone number is not in the format XXX-XXX-XXXX.',
To understand the example you should have already done the Angular translation example. The code below showcases the full telephone rule registration and usage.