-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidator.ts
142 lines (128 loc) · 5.16 KB
/
Validator.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
namespace Basta {
export namespace Utility {
abstract class Validator {
allowEmpty: boolean;
fieldName: string;
constructor(fieldName: string) {
this.allowEmpty = false;
this.fieldName = fieldName;
}
isValid(text: string): boolean {
this.errorMessages.length = 0;
if (!this.allowEmpty && text.length <= 0) {
this.errorMessages.push(this.fieldName + ' should not be empty.');
return false;
}
return true;
}
abstract getErrorMessages(): Array<string>;
protected errorMessages: Array<string> = new Array();
}
export class LettersOnlyValidator extends Validator {
constructor(fieldName: string) {
super(fieldName);
}
isValid(text: string) {
if (!super.isValid(text))
return false;
var pattern: RegExp = new RegExp('^[A-Za-z]+$');
if (!text.match(pattern)) {
this.errorMessages.push(this.fieldName + " should have text only.");
return false;
}
return true;
}
getErrorMessages(): Array<string> {
return this.errorMessages;
}
}
export class NumbersOnlyValidator extends Validator {
constructor(fieldName: string) {
super(fieldName);
}
isValid(text: string) {
if (!super.isValid(text))
return false;
var pattern: RegExp = new RegExp('^[0-9]+$');
if (!text.match(pattern)) {
this.errorMessages.push(this.fieldName+ ' should have number only.');
return false;
}
return true;
}
getErrorMessages(): Array<string> {
return this.errorMessages;
}
}
export class SSNValidator extends Validator {
constructor(fieldName: string) {
super(fieldName);
}
isValid(text: string): boolean {
if (!super.isValid(text))
return false;
var pattern: RegExp = new RegExp('^[0-9]{3}-[0-9]{2}-[0-9]{4}$');
if (!text.match(pattern)) {
this.errorMessages.push(this.fieldName+ ' value is not in correct format. ex: 123-45-6789');
return false;
}
return true;
}
getErrorMessages(): Array<string> {
return this.errorMessages;
}
}
export class EMailValidator extends Validator {
constructor(fieldName: string) {
super(fieldName);
}
isValid(text: string): boolean {
if (!super.isValid(text))
return false;
var atpos = text.indexOf('@');
var dotpos = text.lastIndexOf('.');
if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= text.length) {
this.errorMessages.push(this.fieldName+ ' is not in correct format.');
return false;
}
return true;
}
getErrorMessages(): Array<string> {
return this.errorMessages;
}
}
export class PasswordValidator extends Validator {
public charCount: number = 8;
public maxCharCount: number = 16;
public atLeastOneLetter: boolean = true;
public atLeastOneDigit: boolean = true;
public atLeastOneSpecialChar: boolean = true;
constructor(fieldName: string) {
super(fieldName);
}
isValid(text: string): boolean {
if (!super.isValid(text))
return false;
if (text.length < this.charCount) {
this.errorMessages.push('Your password must be at least 8 characters');
}
if (this.atLeastOneLetter && (text.search(/[a-zA-Z]/)) < 0) {
this.errorMessages.push('Your password must contain at least one letter.');
}
if (this.atLeastOneDigit && (text.search(/[0-9]/) < 0)) {
this.errorMessages.push('Your password must contain at least one digit.');
}
if (this.atLeastOneSpecialChar && (text.search(/[#@$%^&*!()~]/) < 0)) {
this.errorMessages.push('Your password must contain at least one special character.');
}
if (this.errorMessages.length > 0) {
return false;
}
return true;
}
getErrorMessages(): Array<string> {
return this.errorMessages;
}
}
}
}