Last active
December 30, 2015 10:54
-
-
Save qdouble/9c291fb3dee28e0c5539 to your computer and use it in GitHub Desktop.
Custom Form template validator - Angular 2
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, Provider, forwardRef, Attribute} 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'; | |
import {NumberWrapper} from "angular2/src/facade/lang"; | |
/********* Email Validator **********/ | |
export function emailValidator(control: modelModule.Control):{[key: string]: boolean} { | |
console.log('emailValidator processing'); | |
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); } | |
} | |
export const APPLICATION_VALIDATORS = [EmailValidator, NameValidator, AddressValidator, PhoneValidator, ZipCodeValidator]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment