Created
January 19, 2016 18:45
-
-
Save qdouble/21cd8448d882c446aca1 to your computer and use it in GitHub Desktop.
This file contains 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 {Directive, Input, Provider, forwardRef} from 'angular2/core'; | |
import {NG_VALIDATORS, Control, Validator, Validators} from 'angular2/common'; | |
import {CONST_EXPR} from 'angular2/src/facade/lang'; | |
import * as modelModule from 'angular2/src/common/forms/model'; | |
/********* Email Validator **********/ | |
export function emailValidator(control: modelModule.Control): {[key: string]: boolean} { | |
var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i; | |
if (control.value === '' || re.test(control.value)) { | |
return null; | |
} else { | |
return {'invalidEMail': true}; | |
} | |
} | |
@Directive({ | |
selector: '[email-validator]', | |
providers: [ | |
new Provider(NG_VALIDATORS, {useValue: emailValidator, multi: true})] | |
}) | |
export class EmailValidator {} | |
/********* Name Validator **********/ | |
const NAME_VALIDATOR = CONST_EXPR( | |
new Provider(NG_VALIDATORS, {useExisting: forwardRef(() => NameValidator), multi: true})); | |
@Directive({ | |
selector: '[name-validator]', | |
providers: [NAME_VALIDATOR] | |
}) | |
export class NameValidator implements Validator { | |
private _validator: Function; | |
constructor() { | |
this._validator = function (control: modelModule.Control): {[key: string]: boolean} { | |
var re = /^([^<>\/\\*"'$?!;|=@#%^&()_{}:,.~`+]){2,74}$/; | |
if (control.value === '' || re.test(control.value)) { | |
return null; | |
} else { | |
return {invalidName: true}; | |
} | |
}; | |
} | |
validate(c: modelModule.Control): {[key: string]: any} { return this._validator(c); } | |
} | |
/********* Address Validator **********/ | |
const ADDRESS_VALIDATOR = CONST_EXPR( | |
new Provider(NG_VALIDATORS, {useExisting: forwardRef(() => AddressValidator), multi: true})); | |
@Directive({ | |
selector: '[address-validator]', | |
providers: [ADDRESS_VALIDATOR] | |
}) | |
export class AddressValidator implements Validator { | |
private _validator: Function; | |
constructor() { | |
this._validator = function (control: modelModule.Control): {[key: string]: boolean} { | |
var re = /^([^<>\/\\'"&*$;?!|{}=]){2,254}$/; | |
if (control.value === '' || re.test(control.value)) { | |
return null; | |
} else { | |
return {invalidAddress: true}; | |
} | |
}; | |
} | |
validate(c: modelModule.Control): {[key: string]: any} { return this._validator(c); } | |
} | |
/********* Phone Validator **********/ | |
const PHONE_VALIDATOR = CONST_EXPR( | |
new Provider(NG_VALIDATORS, {useExisting: forwardRef(() => PhoneValidator), multi: true})); | |
@Directive({ | |
selector: '[phone-validator]', | |
providers: [PHONE_VALIDATOR] | |
}) | |
export class PhoneValidator implements Validator { | |
private _validator: Function; | |
constructor() { | |
this._validator = function (control: modelModule.Control): {[key: string]: boolean} { | |
var re = /^([0-9Xx ()+.-]){7,30}$/; | |
if (control.value === '' || re.test(control.value)) { | |
return null; | |
} else { | |
return {invalidPhone: true}; | |
} | |
}; | |
} | |
validate(c: modelModule.Control): {[key: string]: any} { return this._validator(c); } | |
} | |
/********* ZipCode Validator **********/ | |
const ZIP_CODE_VALIDATOR = CONST_EXPR( | |
new Provider(NG_VALIDATORS, {useExisting: forwardRef(() => ZipCodeValidator), multi: true})); | |
@Directive({ | |
selector: '[zip-validator]', | |
providers: [ZIP_CODE_VALIDATOR] | |
}) | |
export class ZipCodeValidator implements Validator { | |
private _validator: Function; | |
constructor() { | |
this._validator = function (control: modelModule.Control): {[key: string]: boolean} { | |
var re = /^\d{5}(?:[-\s]\d{4})?$/; | |
if (control.value === '' || re.test(control.value)) { | |
return null; | |
} else { | |
return {invalidZipCode: true}; | |
} | |
}; | |
} | |
validate(c: modelModule.Control): {[key: string]: any} { return this._validator(c); } | |
} | |
/********* Compare Validator **********/ | |
const COMPARE_VALIDATOR = CONST_EXPR( | |
new Provider(NG_VALIDATORS, {useExisting: forwardRef(() => CompareValidator), multi: true})); | |
@Directive({ | |
selector: '[compareValidator]', | |
providers: [COMPARE_VALIDATOR] | |
}) | |
export class CompareValidator implements Validator { | |
@Input() compareValidator: modelModule.Control; | |
private _validator: Function; | |
constructor() { | |
this._validator = function (control: modelModule.Control): any { | |
if (this.compareValidator) { | |
this.compareValidator.valueChanges.subscribe((val) => { compare(); console.log(val); }); | |
} | |
let that = this; | |
function compare () { | |
if (that.compareValidator && (control.value !== that.compareValidator.value)) { | |
return {invalidCompare: true}; | |
} else { | |
control.setErrors(null, control); | |
} | |
} | |
return compare(); | |
}; | |
} | |
validate(c: modelModule.Control): {[key: string]: any} { return this._validator(c); } | |
} | |
/********* Pattern Validator **********/ | |
const PATTERN_VALIDATOR = CONST_EXPR( | |
new Provider(NG_VALIDATORS, {useExisting: forwardRef(() => PatternValidator), multi: true})); | |
@Directive({ | |
selector: '[patternValidator]', | |
providers: [PATTERN_VALIDATOR] | |
}) | |
export class PatternValidator implements Validator { | |
@Input() patternValidator: RegExp; | |
private _validator: Function; | |
constructor() { | |
this._validator = function (control: modelModule.Control): {[key: string]: boolean} { | |
if (control.value === '' || this.patternValidator.test(control.value)) { | |
return null; | |
} else { | |
return {invalidPattern: true}; | |
} | |
}; | |
} | |
validate(c: modelModule.Control): {[key: string]: any} { return this._validator(c); } | |
} | |
/********* Minimum Value Validator **********/ | |
const MIN_VALUE_VALIDATOR = CONST_EXPR( | |
new Provider(NG_VALIDATORS, {useExisting: forwardRef(() => MinValueValidator), multi: true})); | |
@Directive({ | |
selector: '[min]', | |
providers: [MIN_VALUE_VALIDATOR] | |
}) | |
export class MinValueValidator implements Validator { | |
@Input() min: Number; | |
private _validator: Function; | |
constructor() { | |
this._validator = function (control: modelModule.Control): {[key: string]: boolean} { | |
if (control.value === undefined || isNaN(control.value) || this.min < control.value) { | |
return null; | |
} else { | |
return {min: true}; | |
} | |
}; | |
} | |
validate(c: modelModule.Control): {[key: string]: any} { return this._validator(c); } | |
} | |
/********* Maximum Value Validator **********/ | |
const MAX_VALUE_VALIDATOR = CONST_EXPR( | |
new Provider(NG_VALIDATORS, {useExisting: forwardRef(() => MaxValueValidator), multi: true})); | |
@Directive({ | |
selector: '[max]', | |
providers: [MAX_VALUE_VALIDATOR] | |
}) | |
export class MaxValueValidator implements Validator { | |
@Input() max: Number; | |
private _validator: Function; | |
constructor() { | |
this._validator = function (control: modelModule.Control): {[key: string]: boolean} { | |
if (control.value === undefined || isNaN(control.value) || this.max > control.value) { | |
return null; | |
} else { | |
return {max: true}; | |
} | |
}; | |
} | |
validate(c: modelModule.Control): {[key: string]: any} { return this._validator(c); } | |
} | |
export const APPLICATION_VALIDATORS = [EmailValidator, NameValidator, AddressValidator, PhoneValidator, | |
ZipCodeValidator, CompareValidator, PatternValidator, MinValueValidator, MaxValueValidator]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment