-
Notifications
You must be signed in to change notification settings - Fork 0
/
Validation.ts
60 lines (52 loc) · 2.86 KB
/
Validation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { IValidationOptions, INumberValidationOptions, IStringValidationOptions, ITimeValidationOptions } from './IValidationOptions';
import { CreditCardType, CreditCardValidator } from './CreditCardValidator';
import { PostcodeLocale, PostcodeValidator } from './PostcodeValidator';
import * as Moment from "moment";
export class Validation {
private static _validationRules: any = {
Number: ( value: number, options: INumberValidationOptions ) => {
if( typeof value !== "number" )
return false;
if( options.Max !== undefined && options.Min !== undefined )
return value < options.Max && value > options.Min;
return ( options.Max !== undefined && value < options.Max ) ||
( options.Min !== undefined && value > options.Min );
},
String: ( value: string , options: IStringValidationOptions ) => {
if( typeof value !== "string" )
return false;
if( options.MaxLength !== undefined && options.MinLength !== undefined )
return value.length < options.MaxLength && value.length > options.MinLength;
return ( options.MaxLength !== undefined && value.length < options.MaxLength ) ||
( options.MinLength !== undefined && value.length > options.MinLength );
},
Email: ( value: string, b ) => {
return ( typeof value === "string" && /^(([^<>()\[\]\\.,;:\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,}))$/.test( value ) );
},
CreditCard: ( value: string, options: CreditCardType[] ) => {
return CreditCardValidator.Validate( value, options );
},
Pattern: ( value: any, regex: RegExp ) => {
return regex.test( value );
},
URL: ( value: any ) => {
return /\(?(?:(http|https|ftp):\/\/)?(?:((?:[^\W\s]|\.|-|[:]{1})+)@{1})?((?:www.)?(?:[^\W\s]|\.|-)+[\.][^\W\s]{2,4}|localhost(?=\/)|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?::(\d*))?([\/]?[^\s\?]*[\/]{1})*(?:\/?([^\s\n\?\[\]\{\}\#]*(?:(?=\.)){1}|[^\s\n\?\[\]\{\}\.\#]*)?([\.]{1}[^\s\?\#]*)?)?(?:\?{1}([^\s\n\#\[\]]*))?([\#][^\s\n]*)?\)?/gi.test( value );
},
Time: ( value:string, options:ITimeValidationOptions ) => {
return Moment( value, options.Format, options.Locale ).isValid();
},
Postcode: (value: string, options: PostcodeLocale[]) => {
return PostcodeValidator.Validate(value, options);
}
};
constructor( private _options: IValidationOptions ) {
}
Validate( value: any ): boolean {
for( var key in this._options ) {
if( Validation._validationRules[ key ]( value, this._options[ key ] ) === false ) {
return false;
}
}
return true;
}
}