ngx-input-validator
Input validator Angular reactive forms inputs.
Installation
NPM:
$ npm install ngx-input-validator
Usage:
import { Validator } from 'ngx-input-validator';
Component:
formGroup: FormGroup; thisformGroup = _fb; ifform ;
HTML:
Invalid email.
Features
Here are some useful input validators provided in this package:
- amountValidator
Works on AbstractControl, for validating amount as string, use validatePrice.
let amountInput: FormControl = 0 ValidatoramountValidator;
Returns { 'invalidAmount': true } if control value is invalid, otherwise in case of no error returns null.
- trimSpaces
To remove spaces from start and end of a string.
let value: string = Validator;
Returns trimmed value as String.
- validatePrice
For validating amount in dollars in form of string.
let priceInput: FormControl = 0 ValidatorvalidatePrice;
Returns true for valid amount and false for invalid amount value.
- validateJSON
Validates JSON string.
let jsonInput: FormControl = '' ValidatorvalidateJSON;
Returns true in case of valid stringified JSON, otherwise false.
- creditCardValidator
Works on AbstractControl, to validate Visa, MasterCard, American Express, Diners Club, Discover, JCB etc.. card number.
let creditCardInput: FormControl = '' ValidatorcreditCardValidator;
Returns { 'invalidCreditCard': true } as error in case of invalid card number, otherwise null.
- emailValidator
For AbstractControl, enhanced form of basic email validator.
let emailInput: FormControl = '' ValidatoremailValidator;
Returns { 'invalidEmailAddress': true } as error in case of invalid email address, otherwise null.
- phoneValidator
For AbstractControl, validates 10 digit phone number which may includes hypens (-).
let phoneInput: FormControl = null ValidatorphoneValidator;
Returns { 'invalidPhoneNumber': true } as error in case of invalid phone number, otherwise null.
- mobileValidator
For AbstractControl, validates 8-15 digit phone number which includes 1-3 digit prefix number spearted by a hypen (-).
let phoneInput: FormControl = null ValidatorphoneValidator;
Returns { 'invalidPhoneNumber': true } as error in case of invalid phone number, otherwise null.
- imeiValidator
For AbstractControl, validates IMEI number.
let imeiInput: FormControl = null ValidatorimeiValidator;
Returns { 'invalidIMEINumber': true } as error in case of invalid IMEI number, otherwise null.
- vinValidator
For AbstractControl, validates vehicle VIN number.
let vinInput: FormControl = null ValidatorvinValidator;
Returns { 'invalidVinNumber': true } as error in case of invalid VIN number, otherwise null.
- url
For AbstractControl, validates http/https URL.
let urlInput: FormControl = '' Validatorurl;
Returns { 'invalidurl': true } as error in case of invalid http/https URL, otherwise null.
- passwordValidator
For AbstractControl, validates minimum 8 characters password which may include any character.
let passwordInput: FormControl = null ValidatorpasswordValidator;
Returns { 'invalidPassword': true } as error in case of invalid password, otherwise null.
- ValidateFile
For AbstractControl, validates jpg, jpeg and png extension file.
let fileInput: FormControl = null ValidatorValidateFile;
Returns { 'invalidImage': true } as error in case of invalid image, otherwise null.
- ValidatePngFile
For AbstractControl, validates png extension file.
let fileInput: FormControl = null ValidatorValidatePngFile;
Returns { 'invalidImage': true } as error in case of invalid image, otherwise null.
- ageValidator
For AbstractControl, validates age between 0 to 119.
let ageInput: FormControl = 0 ValidatorageValidator;
Returns { 'invalidAge': true } as error in case of invalid age year number, otherwise null.
- required
For AbstractControl, validates age between 0 to 119.
let requiredInput: FormControl = null Validatorrequired;
Returns { required: true } as error in case of blank input, otherwise null.
- timeFormat
For AbstractControl, validates time in format of '{H}H:{M}M', where digits in {} are optional.
let timeInput: FormControl = '' ValidatortimeFormat;
Returns { 'invalidTimeFormat': true } as error in case valid time format, otherwise null.
- matchingPasswords
Matches two password inputs.
; }
Returns { Equivalent: true } if password inputs match otherwise returns { notEquivalent: true }
Issues
If you find a bug, please file an issue on our issue tracker on GitHub.
Credits
Boring Devs