validation.js 5.33 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * String utilities [ end ]
 */

// =============================================================================================
// Utils for string, date, number [ end ]
// =============================================================================================
var ValidationUtil = {

10
    // Required Text
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
    CheckRequiredForText : function(value) {

        if (value == null || value == '') {
            return false;
        }
        return true;
    },

    // get byte count
    GetByteCount : function(value) {
        var escapedStr = encodeURI(value);
        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);
            count = count + tmp;
        } else {
            count = escapedStr.length;
        }
        return count;
    },

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

41
    // check text max length
42 43 44 45 46 47 48
    CheckMaxLengthForByte : function(value, len) {

        if (this.GetByteCount(value) > len)
            return false;
        return true;
    },

49
    // check if the text is a number
50 51 52 53 54
    IsNumber : function(value) {
        var reg = new RegExp("^[0-9]+$");
        return reg.test(value);
    },

55
    // check if the text is a alphabet
56 57 58 59 60
    IsAlphabet : function(value) {
        var reg = new RegExp("^[a-zA-Z]+$");
        return reg.test(value);
    },

61
    // check if the text is a symbol
62 63 64 65 66 67
    IsSymbol : function(value) {
        var reg = new RegExp(
                "\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");
        return !reg.test(value);
    },

68
    // check if the text is a emailAddress
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 111 112
    CheckEmailValid : function(value) {
        // Check if string is a valid email address
        var reg = new RegExp("^[0-9a-zA-Z]+@[0-9a-zA-Z]+[\\.]{1}[0-9a-zA-Z]+[\\.]?[0-9a-zA-Z]+$");
        return reg.test(value);
    },
    // Password
    CheckPasswordValid : function(value) {
        // Check if string is a valid email address
        var reg = new RegExp("^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[#+-\\./:_]).{1,47}$");
        return reg.test(value);
    },
    // Alphabet + Nunber + Symbol
    IsAlphabetOrNumberOrSymbol : function(value) {
        // Check if string is alphabet or number or symbol
        var reg = new RegExp(
                "\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");
        return !reg.test(value);
    },
    // Get the total of types in array
    CheckNumberOfTypeInString : function(value) {
        var list = new Array();
        var c;
        for ( var i = 0; i < value.length; i++) {
            c = value[i];
            for ( var j = i + 1; j < value.length; j++) {
                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;
        return count
    },
    // Special character: *
    IsCharacterSpecial : function(value) {
        for ( var i = 0; i < value.length; i++) {
            if (value[i] == '*')
                return true;
        }
        return false;
    },

113
    // Symbol check for password
114 115 116 117 118
    IsPasswordSymbol : function(value) {
        var reg = new RegExp("\u005b\u005e\u0027\u0023\u0027\u002b\u005c\u002d\u0027\u002e\u0027\u002f\u0027\u003a\u0027\u005f\u005d");
        return !reg.test(value);
    },

119 120
    // text check for password(Alphabet Or Number Or Symbol)
    IsPasswordAlphabetOrNumberOrSymbol : function(value) {
121 122 123 124
        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");
        return !reg.test(value);
    },

125 126
    // Check at consecutive characters
    HasSeqChar : function(str, num) {
127 128 129
    	var count = 0;
    	var prev = 0;

130
    	for (var i = 0; i < str.length; i++) {
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
			var c = str.charAt(i);
			if (i > 0 && prev == c) {
				count++;
				if (count == num - 1) {
					return true;
				}
			}
			else {
				count = 0;
			}
			prev = c;
		}

		return false;
    },

147
    // Check same characters in text
148 149 150 151 152 153 154 155 156 157 158 159 160 161
    ContainSameSeqChar : function(str1, str2, num) {
    	if (str2.length < num || str1.length < num) {
			return false;
		}

		for (var i = 0; i <= str2.length - num; i++) {
			var target = str2.substring(i, i + num);
			if (str1.contains(target)) {
				return true;
			}
		}
		return false;
    }
};