Passed
Pull Request — master (#211)
by
unknown
15:53
created

Resources/Public/JavaScript/formvalidators.js   A

Complexity

Total Complexity 12
Complexity/F 4

Size

Lines of Code 45
Function Count 3

Duplication

Duplicated Lines 4
Ratio 8.89 %

Importance

Changes 0
Metric Value
wmc 12
eloc 34
c 0
b 0
f 0
dl 4
loc 45
rs 10
mnd 9
bc 9
fnc 3
bpm 3
cpm 4
noi 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
var isDate = function(value) {
2
  if (value == "") return false;
3
  var rxDatePattern = /^(\d{1,2})(\.)(\d{1,2})(\.)(\d{4})$/; //Declare Regex
4
  var dtArray = value.match(rxDatePattern); // is format OK?
5
  if (dtArray == null) return false;
0 ignored issues
show
Best Practice introduced by
Comparing dtArray to null using the == operator is not safe. Consider using === instead.
Loading history...
6
  //Checks for mm/dd/yyyy format.
7
  var dtMonth = dtArray[3];
8
  var dtDay = dtArray[1];
9
  var dtYear = dtArray[5];
10
  if (dtMonth < 1 || dtMonth > 12) {
11
    return false;
12
  } else if (dtDay < 1 || dtDay > 31) {
13
    return false;
14
  } else if ((dtMonth == 4 || dtMonth == 6 || dtMonth == 9 || dtMonth == 11) && dtDay == 31) {
15
    return false;
16 View Code Duplication
  } else if (dtMonth == 2) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
17
    var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
0 ignored issues
show
Best Practice introduced by
Comparing dtYear % 400 to 0 using the == operator is not safe. Consider using === instead.
Loading history...
Best Practice introduced by
Comparing dtYear % 4 to 0 using the == operator is not safe. Consider using === instead.
Loading history...
Best Practice introduced by
Comparing dtYear % 100 to 0 using the != operator is not safe. Consider using !== instead.
Loading history...
18
    if (dtDay > 29 || (dtDay == 29 && !isleap)) return false;
19
  }
20
  return true;
21
}
22
23
var dateValidator = {
24
  type: 'DATE',
25
  validate(value, validationExpression = null) {
26
    return !value || value.length == 0 || isDate(value);
0 ignored issues
show
Best Practice introduced by
Comparing value.length to 0 using the == operator is not safe. Consider using === instead.
Loading history...
27
  }
28
}
29
30
var regexpValidator = {
31
  type: 'REGEXP',
32
  validate(value, validationExpression = null) {
33
    if (value && value.length > 0 && validationExpression && validationExpression.length > 0) {
34
      try {
35
        let regexp = new RegExp(validationExpression);
36
        let res = value.match(regexp);
37
        return res && res.length == 1 && res[0] == value;
0 ignored issues
show
Best Practice introduced by
Comparing res.length to 1 using the == operator is not safe. Consider using === instead.
Loading history...
38
      } catch (err) {
39
        return false;
40
      }
41
    } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
42
      return true;
43
    }
44
  }
45
}
46