-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.validators.ts
94 lines (80 loc) · 2.51 KB
/
sample.validators.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
import { FormGroup } from '@angular/forms';
//these are sample custom validators
//password match validator
export function PasswordMatch(controlName: string, matchingControlName: string){
return(formGroup: FormGroup)=>{
const control = formGroup.controls[controlName];
const matchingControl = formGroup.controls[matchingControlName]
if (matchingControl.errors && !matchingControl.errors.mustMatch) {
return;
}
if(control.value !== matchingControl.value){
matchingControl.setErrors({ passwordMatch: true})
}else{
matchingControl.setErrors(null)
}
}
}
//checks if latitude value is valid
export function LatitudeCheck(controlName: string){
return(formGroup: FormGroup)=>{
const control = formGroup.controls[controlName];
const check = Math.abs(control.value)
if (control.errors) {
return;
}
if(check && check <= 90 || check === 0){
control.setErrors(null)
}else{
control.setErrors({ latitudeCheck: true})
}
}
}
//checks if lognitude value is valid
export function LongitudeCheck(controlName: string){
return(formGroup: FormGroup)=>{
const control = formGroup.controls[controlName];
const check = Math.abs(control.value)
if (control.errors) {
return;
}
if(check && check <= 180 || check === 0){
control.setErrors(null)
}else{
control.setErrors({ longitudeCheck: true})
}
}
}
//checks size of given element if its a valid number
export function SizeCheck(controlName: string){
return(formGroup: FormGroup)=>{
const control = formGroup.controls[controlName];
if (control.errors) {
return;
}
if(isNaN(Number(control.value))){
control.setErrors({ sizeCheck: true})
}else{
control.setErrors(null)
}
}
}
//validates file size
onSelect(myFile, element: HTMLElement,){
if(myFile.length > 0){
if(element.getAttribute('formControlName') === 'landone'){
if(myFile[0].size > 5120000){
this.f.landone.setErrors({limitSize: true})
}else{
this.fileone = myFile[0]
}
}
else if(element.getAttribute('formControlName') === 'landtwo'){
if(myFile[0].size > 5120000){
this.f.landtwo.setErrors({limitSize: true})
}else{
this.filetwo = myFile[0]
}
}
}
}