-
Notifications
You must be signed in to change notification settings - Fork 0
/
paramvalidate.js
83 lines (59 loc) · 2.03 KB
/
paramvalidate.js
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
//check if currency requested is available
var checkmatch = (cur) => {
var _input = cur.toUpperCase();
var curArray = ['USD','GBP','EUR','TRY','TND','CNY'];
var status = curArray.includes(_input);
return(status);
}
//validate if the date provided matches the YYYY-MM-DD format, returns true or false
var isdate = (date)=> {
status = false;
if (date.includes('-') & date.length == 10) {
datearray = date.split('-')
if (datearray.length == 3){
status = true;
}}
return(status);
}
//check if the Date provided is convertable to a date, retutrns true or false
var convertable = (date)=> {
status = true;
var dateswitch = new Date(date)
var ifdate = String(dateswitch)
if (ifdate == "Invalid Date"){
status = false;
}
return(status)
}
//combining both date validation techniques in one function, return true or false
var datevalidate = (date)=>{
if (date != 'Today' & date != 'today'){
dateconvertable = convertable(date);
stringisdate = isdate(date);
if (dateconvertable == true & stringisdate == true){
return(true)
}
else{return(false)}
}
else if(date == 'Today' || date == 'today'){ return(true)}
else{return(false)}
}
//a function which returns a status code for the validation of date and currency inputs.
var validateCurrencyAndDate = (currency,date)=>{
//response codes: 1 : successful , 2 : invalid cur, 3 : invalid date
var Curvalidation = checkmatch(currency);
var Datevalidation = datevalidate(date);
validationstatus = '1'
if (Curvalidation == false){
validationstatus = '2'
};
if (Datevalidation == false){
validationstatus = '3'
};
//if (reponsestatus == '2'){Answer = responses.currencynotin}
//else if (reponsestatus == '3'){Answer = responses.notaDate}
return(validationstatus);
}
exports.checkmatch = checkmatch;
exports.datevalidate = datevalidate;
exports.validateCurrencyAndDate = validateCurrencyAndDate;