validation.js 5.51 KB
Newer Older
Takumi Imai committed
1
/**
2
 * String utilities [ end ]
Takumi Imai committed
3 4
 *
 * @since cms:1.4.3.2&1.4.3.3 web:1.0
5 6 7 8 9 10
 */

// =============================================================================================
// Utils for string, date, number [ end ]
// =============================================================================================
var ValidationUtil = {
11
    // Required Text
Takumi Imai committed
12
    CheckRequiredForText: function (value) {
13 14 15 16 17 18 19
        if (value == null || value == '') {
            return false;
        }
        return true;
    },

    // get byte count
Takumi Imai committed
20
    GetByteCount: function (value) {
21
        var escapedStr = encodeURI(value);
Takumi Imai committed
22 23 24 25
        if (escapedStr.indexOf('%') != -1) {
            var count = escapedStr.split('%').length - 1;
            if (count == 0) count++; // perverse case; can't happen with real UTF-8
            var tmp = escapedStr.length - count * 3;
26 27 28 29 30 31 32
            count = count + tmp;
        } else {
            count = escapedStr.length;
        }
        return count;
    },

33
    // check text min length
Takumi Imai committed
34 35
    CheckMinLengthForByte: function (value, len) {
        if (this.GetByteCount(value) < len) return false;
36 37 38
        return true;
    },

39
    // check text max length
Takumi Imai committed
40 41
    CheckMaxLengthForByte: function (value, len) {
        if (this.GetByteCount(value) > len) return false;
42 43 44
        return true;
    },

45
    // check if the text is a number
Takumi Imai committed
46 47
    IsNumber: function (value) {
        var reg = new RegExp('^[0-9]+$');
48 49 50
        return reg.test(value);
    },

51
    // check if the text is a alphabet
Takumi Imai committed
52 53
    IsAlphabet: function (value) {
        var reg = new RegExp('^[a-zA-Z]+$');
54 55 56
        return reg.test(value);
    },

57
    // check if the text is a symbol
Takumi Imai committed
58
    IsSymbol: function (value) {
59
        var reg = new RegExp(
Takumi Imai committed
60 61
            '\u005b\u005e\u0027\u0060\u0027\u007e\u0027\u0021\u0027\u0040\u0027\u0023\u0027\u0024\u0027\u0025\u0027\u005e\u0027\u0026\u0027\u002a\u0027\u0028\u0027\u0029\u0027\u005f\u0027\u002b\u0027\u003d\u0027\u007b\u0027\u007d\u0027\u007c\u0027\u003a\u0027\u0022\u0027\u003b\u0027\u0027\u0027\u003c\u0027\u003e\u0027\u003f\u0027\u002f\u0027\u002e\u0027\u002c\u005c\u002d\u005b\u005c\u005d\u005c\u005c\u005d',
        );
62 63 64
        return !reg.test(value);
    },

65
    // check if the text is a emailAddress
Takumi Imai committed
66
    CheckEmailValid: function (value) {
67
        // Check if string is a valid email address
Takumi Imai committed
68
        var reg = new RegExp('^[0-9a-zA-Z]+@[0-9a-zA-Z]+[\\.]{1}[0-9a-zA-Z]+[\\.]?[0-9a-zA-Z]+$');
69 70 71
        return reg.test(value);
    },
    // Password
Takumi Imai committed
72
    CheckPasswordValid: function (value) {
73
        // Check if string is a valid email address
Takumi Imai committed
74
        var reg = new RegExp('^(?=.*d)(?=.*[a-z])(?=.*[A-Z])(?=.*[#+-\\./:_]).{1,47}$');
75 76 77
        return reg.test(value);
    },
    // Alphabet + Nunber + Symbol
Takumi Imai committed
78
    IsAlphabetOrNumberOrSymbol: function (value) {
79 80
        // Check if string is alphabet or number or symbol
        var reg = new RegExp(
Takumi Imai committed
81 82
            '\u005b\u005e\u0061\u002d\u007a\u0041\u002d\u005a\u0030\u002d\u0039\u0027\u0060\u0027\u007e\u0027\u0021\u0027\u0040\u0027\u0023\u0027\u0024\u0027\u0025\u0027\u005e\u0027\u0026\u0027\u002a\u0027\u0028\u0027\u0029\u0027\u005f\u0027\u002b\u0027\u003d\u0027\u007b\u0027\u007d\u0027\u007c\u0027\u003a\u0027\u0022\u0027\u003b\u0027\u0027\u0027\u003c\u0027\u003e\u0027\u003f\u0027\u002f\u0027\u002e\u0027\u002c\u005c\u002d\u005b\u005c\u005d\u005c\u005c\u005d',
        );
83 84 85
        return !reg.test(value);
    },
    // Get the total of types in array
Takumi Imai committed
86
    CheckNumberOfTypeInString: function (value) {
87 88
        var list = new Array();
        var c;
Takumi Imai committed
89
        for (var i = 0; i < value.length; i++) {
90
            c = value[i];
Takumi Imai committed
91
            for (var j = i + 1; j < value.length; j++) {
92 93 94 95 96 97 98 99
                if (value[j] == c) {
                    value = value.slice(0, j) + value.slice(j + 1, value.len);
                    j = j - 1;
                }
            }
            list[i] = c;
        }
        var count = list.length;
Takumi Imai committed
100
        return count;
101 102
    },
    // Special character: *
Takumi Imai committed
103 104 105
    IsCharacterSpecial: function (value) {
        for (var i = 0; i < value.length; i++) {
            if (value[i] == '*') return true;
106 107 108 109
        }
        return false;
    },

110
    // Symbol check for password
Takumi Imai committed
111 112
    IsPasswordSymbol: function (value) {
        var reg = new RegExp('\u005b\u005e\u0027\u0023\u0027\u002b\u005c\u002d\u0027\u002e\u0027\u002f\u0027\u003a\u0027\u005f\u005d');
113 114 115
        return !reg.test(value);
    },

116
    // text check for password(Alphabet Or Number Or Symbol)
Takumi Imai committed
117 118
    IsPasswordAlphabetOrNumberOrSymbol: function (value) {
        var reg = new RegExp('\u005b\u005e\u0061\u002d\u007a\u0041\u002d\u005a\u0030\u002d\u0039\u0027\u0023\u0027\u002b\u005c\u002d\u0027\u002e\u0027\u002f\u0027\u003a\u0027\u005f\u005d');
119 120 121
        return !reg.test(value);
    },

122
    // Check at consecutive characters
Takumi Imai committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    HasSeqChar: function (str, num) {
        var count = 0;
        var prev = 0;

        for (var i = 0; i < str.length; i++) {
            var c = str.charAt(i);
            if (i > 0 && prev == c) {
                count++;
                if (count == num - 1) {
                    return true;
                }
            } else {
                count = 0;
            }
            prev = c;
        }
139

Takumi Imai committed
140
        return false;
141 142
    },

143
    // Check same characters in text
Takumi Imai committed
144 145 146 147
    ContainSameSeqChar: function (str1, str2, num) {
        if (str2.length < num || str1.length < num) {
            return false;
        }
148

Takumi Imai committed
149 150 151 152 153 154 155 156 157
        for (var i = 0; i <= str2.length - num; i++) {
            var target = str2.substring(i, i + num);
            if (str1.contains(target)) {
                return true;
            }
        }
        return false;
    },
};