Skip to content

Instantly share code, notes, and snippets.

@aegyed91
Created December 19, 2016 13:20
Show Gist options
  • Save aegyed91/2a894e1420d9dad1dfbed89d3269df28 to your computer and use it in GitHub Desktop.
Save aegyed91/2a894e1420d9dad1dfbed89d3269df28 to your computer and use it in GitHub Desktop.
validators
import { ValidatorFn, AbstractControl } from '@angular/forms';
/**
* All file sizes specified in bytes.
*/
export function emailValidator(): ValidatorFn {
return (control: AbstractControl): {[key: string]: any} => {
const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
const passed = re.test(control.value);
return passed ? null : { invalidEmail: true };
};
}
import { ValidatorFn, AbstractControl } from '@angular/forms';
import { isNumber, isNull, isEmpty } from 'lodash';
export function requiredValidator(): ValidatorFn {
return (control: AbstractControl): {[key: string]: any} => {
if (isNumber(control.value)) {
return null;
}
if (isNull(control.value) || isEmpty(control.value)) {
return {required: true};
}
return null;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment