Created
December 19, 2016 13:20
-
-
Save aegyed91/2a894e1420d9dad1dfbed89d3269df28 to your computer and use it in GitHub Desktop.
validators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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