aboutsummaryrefslogtreecommitdiffstats
path: root/src/Validators.js
blob: b7efcb4ab68392a33d1f9446c6e5d0972e2d9c31 (plain)
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
foreach({
  'regexp': function(value, regexp, msg) {
    if (!value.match(regexp)) {
      return msg ||
        "Value does not match expected format " + regexp + ".";
    } else {
      return null;
    }
  },
  
  'number': function(value, min, max) {
    var num = 1 * value;
    if (num == value) {
      if (typeof min != 'undefined' && num < min) {
        return "Value can not be less than " + min + ".";
      }
      if (typeof min != 'undefined' && num > max) {
        return "Value can not be greater than " + max + ".";
      }
      return null;
    } else {
      return "Value is not a number.";
    }
  },
  
  'integer': function(value, min, max) {
    var numberError = angularValidator['number'](value, min, max);
    if (numberError) return numberError;
    if (!("" + value).match(/^\s*[\d+]*\s*$/) || value != Math.round(value)) {
      return "Value is not a whole number.";
    }
    return null;
  },
  
  'date': function(value, min, max) {
    if (value.match(/^\d\d?\/\d\d?\/\d\d\d\d$/)) {
      return null;
    }
    return "Value is not a date. (Expecting format: 12/31/2009).";
  },
  
  'ssn': function(value) {
    if (value.match(/^\d\d\d-\d\d-\d\d\d\d$/)) {
      return null;
    }
    return "SSN needs to be in 999-99-9999 format.";
  },
  
  'email': function(value) {
    if (value.match(/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/)) {
      return null;
    }
    return "Email needs to be in username@host.com format.";
  },
  
  'phone': function(value) {
    if (value.match(/^1\(\d\d\d\)\d\d\d-\d\d\d\d$/)) {
      return null;
    }
    if (value.match(/^\+\d{2,3} (\(\d{1,5}\))?[\d ]+\d$/)) {
      return null;
    }
    return "Phone number needs to be in 1(987)654-3210 format in North America or +999 (123) 45678 906 internationaly.";
  },
  
  'url': function(value) {
    if (value.match(/^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/)) {
      return null;
    }
    return "URL needs to be in http://server[:port]/path format.";
  },
  
  'json': function(value) {
    try {
      fromJson(value);
      return null;
    } catch (e) {
      return e.toString();
    }
  },
  
  'asynchronous': function(text, asynchronousFn) {
    var stateKey = '$validateState';
    var lastKey = '$lastKey';
    var obj = this['$element'];
    var stateCache = obj[stateKey] = obj[stateKey] || {};
    var state = stateCache[text];
    var updateView = this['$updateView'];
    obj[lastKey] = text;
    if (state === undefined) {
      // we have never seen this before, Request it
      jQuery(obj).addClass('ng-input-indicator-wait');
      state = stateCache[text] = null;
      asynchronousFn(text, function(error){
        state = stateCache[text] = error ? error : false;
        if (stateCache[obj[lastKey]] !== null) {
          jQuery(obj).removeClass('ng-input-indicator-wait');
        }
        updateView();
      });
    }

    if (state === null){
      // request in flight, mark widget invalid, but don't show it to user
      this['$invalidWidgets'].push(this.$element);
    }
    return state;
  }

}, function(v,k) {angularValidator[k] = v;});