1 /**
  2  *
  3  * string方法
  4  * @fileOverView core/string
  5  * @author  <a href="mailto:zhang.gd@foxmail.com">Zhang Guangda</a>
  6  * @date    2012-10-25
  7  */
  8 define("core/string", function() {
  9 	/**
 10 	 * @class we string方法
 11 	 */
 12 	$we.string = {
 13 		/**
 14 		 * 获取字符串当中的数字
 15 		 * @param  {string} str 字符串
 16 		 * @return {integer}     数字
 17 		 */
 18 		getInt: function(str){
 19 			var regex = new RegExp("(\\d+)");
 20 			var numbs = regex.exec(str);
 21 			if(numbs && numbs.length >= 2){
 22 				return parseInt(numbs[1]);
 23 			};
 24 			return 0;
 25 		},
 26 
 27 		/**
 28 		 * 增加对象中的数字
 29 		 * @param {dom} el  element
 30 		 * @param {integer} num 增加的数值
 31 		 */
 32 		addStrNum: function(el, num){
 33 			var v;
 34 			if (typeof el == "string") {
 35 				v = el;
 36 			} else if (typeof el =="object" && el.innerHTML) {
 37 				v = el.innerHTML;
 38 			} else {
 39 				return "";
 40 			}
 41 
 42 			var n1 = $we.string.getInt(v);
 43 			var n2 = n1 + num;
 44 			v = v.replace(n1, n2);
 45 			try{
 46 				if(typeof el =="object" && el.innerHTML) {
 47 					el.innerHTML = v;
 48 				}
 49 			}catch(e){}
 50 			return v;
 51 		},
 52 		/**
 53 		 * 字符串掩码
 54 		 * @param  {string} str    字符串
 55 		 * @param  {string} symbol 掩码,默认是 *
 56 		 * @return {string}        字符串
 57 		 */
 58 		maskString: function(str, symbol) {
 59 			var mask = "",
 60 				symbol = symbol || '*',
 61 				atIndex = str.indexOf("@");
 62 
 63 
 64 			if (atIndex != -1) {
 65 				return this.maskString(str.substr(0, atIndex)) + str.substr(atIndex);
 66 			}
 67 			if (str.length > 7) {
 68 				for (var i = 0; i < 5; ++i) {
 69 					mask += symbol;
 70 				};
 71 				return str.substr(0, 3) + mask + str.substr(str.length - 3);
 72 			}
 73 			if (str.length > 4) {
 74 				for (var i = 0; i < 3; ++i) {
 75 					mask += symbol;
 76 				};
 77 				return str.substr(0, 2) + mask + str.substr(str.length - 1);
 78 			}
 79 			if (str.length > 2) {
 80 				for (var i = 0; i < 2; ++i) {
 81 					mask += symbol;
 82 				};
 83 				return str.substr(0, 1) + mask + str.substr(str.length - 1);
 84 			}
 85 			return str;
 86 		},
 87 		/**
 88 		 * 替换模板,$str$
 89 		 * @param  {string} str  模板字符串
 90 		 * @param  {object} conf 模板替换配置
 91 		 * @return {string}      字符串
 92 		 */
 93 		replaceTmpl: function(str, conf) {
 94 			return("" + str).replace(/\$(\w+)\$/g, function(a, b) {
 95 				return typeof conf[b] != "undefined" ? conf[b] : "$" + b + "$"
 96 			});
 97 		},
 98 		/**
 99 		 * 获取hash
100 		 * @return {string} 当前hash
101 		 */
102 		getHash: function() {
103 			return window.location.hash.substr(1);
104 		},
105 		/**
106 		 * 设置hash
107 		 * @param {string} name hash的名字
108 		 */
109 		setHash: function(name) {
110 			window.location.hash = name;
111 		},
112 
113 		/**
114 		 * 获取QueryString的数组
115 		 * @return {Object} 返回值
116 		 */
117 		getQueryString: function() {
118 			var result = location.search.match(new RegExp("[\?\&][^\?\&]+=[^\?\&]+", "g"));
119 			for(var i = 0; i < result.length; i++) {
120 				result[i] = result[i].substring(1);
121 			}
122 			return result;
123 		},
124 		/**
125 		 * 根据QueryString参数名称获取值
126 		 * @param  {string} name 需要获取的参数
127 		 * @return {string}      参数的值
128 		 */
129 		getQueryByName: function(name) {
130 			var result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
131 			if(result == null || result.length < 1) {
132 				return "";
133 			}
134 			return result[1];
135 		},
136 		/**
137 		 * 根据QueryString参数索引获取值
138 		 * @param  {number} index 参数索引
139 		 * @return {string}       参数的值
140 		 */
141 		getQueryByIndex: function(index) {
142 			if(index == null) {
143 				return "";
144 			}
145 			var queryStringList = getQueryString();
146 			if(index >= queryStringList.length) {
147 				return "";
148 			}
149 			var result = queryStringList[index];
150 			var startIndex = result.indexOf("=") + 1;
151 			result = result.substring(startIndex);
152 			return result;
153 		},
154 	    /**
155 	     * To String
156 	     * @param obj
157 	     */
158 	    toString:function(obj){
159 
160 	    },
161 
162 	    /**
163 	     * 验证省份证号码是否有效
164 	     * @param  {String} cardNumber 身份证号码
165 	     * @return {Boolean}            是否是正确的身份证号
166 	     */
167 	    validIdCard:function(cardNumber){
168 	        if (cardNumber.length > 0) {
169 	        	var vcity = { 11:"北京", 12:"天津", 13:"河北", 14:"山西", 15:"内蒙古",
170 	        	    21:"辽宁", 22:"吉林", 23:"黑龙江", 31:"上海", 32:"江苏",
171 	        	    33:"浙江", 34:"安徽", 35:"福建", 36:"江西", 37:"山东", 41:"河南",
172 	        	    42:"湖北", 43:"湖南", 44:"广东", 45:"广西", 46:"海南", 50:"重庆",
173 	        	    51:"四川", 52:"贵州", 53:"云南", 54:"西藏", 61:"陕西", 62:"甘肃",
174 	        	    63:"青海", 64:"宁夏", 65:"新疆", 71:"台湾", 81:"香港", 82:"澳门", 91:"国外"
175 	        	};
176 
177 	        	//检查号码是否符合规范,包括长度,类型
178 	        	var isCardNo = function (card)
179 	        	{
180 	        	    //身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X
181 	        	    var reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/;
182 	        	    if (reg.test(card) === false) {
183 	        	        return false;
184 	        	    }
185 	        	    return true;
186 	        	};
187 
188 	        	//取身份证前两位,校验省份
189 	        	var checkProvince = function (card)
190 	        	{
191 	        	    var province = card.substr(0, 2);
192 	        	    if (vcity[province] == undefined) {
193 	        	        return false;
194 	        	    }
195 	        	    return true;
196 	        	};
197 
198 	        	//检查生日是否正确
199 	        	var checkBirthday = function (card)
200 	        	{
201 	        	    var len = card.length;
202 	        	    //身份证15位时,次序为省(3位)市(3位)年(2位)月(2位)日(2位)校验位(3位),皆为数字
203 	        	    if (len == '15') {
204 	        	        var re_fifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/;
205 	        	        var arr_data = card.match(re_fifteen);
206 	        	        var year = arr_data[2];
207 	        	        var month = arr_data[3];
208 	        	        var day = arr_data[4];
209 	        	        var birthday = new Date('19' + year + '/' + month + '/' + day);
210 	        	        return verifyBirthday('19' + year, month, day, birthday);
211 	        	    }
212 	        	    //身份证18位时,次序为省(3位)市(3位)年(4位)月(2位)日(2位)校验位(4位),校验位末尾可能为X
213 	        	    if (len == '18') {
214 	        	        var re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/;
215 	        	        var arr_data = card.match(re_eighteen);
216 	        	        var year = arr_data[2];
217 	        	        var month = arr_data[3];
218 	        	        var day = arr_data[4];
219 	        	        var birthday = new Date(year + '/' + month + '/' + day);
220 	        	        return verifyBirthday(year, month, day, birthday);
221 	        	    }
222 	        	    return false;
223 	        	};
224 
225 	        	//校验日期
226 	        	var verifyBirthday = function (year, month, day, birthday)
227 	        	{
228 	        	    var now = new Date();
229 	        	    var now_year = now.getFullYear();
230 	        	    //年月日是否合理
231 	        	    if (birthday.getFullYear() == year && (birthday.getMonth() + 1) == month && birthday.getDate() == day) {
232 	        	        //判断年份的范围(3岁到100岁之间)
233 	        	        var time = now_year - year;
234 	        	        if (time >= 3 && time <= 100) {
235 	        	            return true;
236 	        	        }
237 	        	        return false;
238 	        	    }
239 	        	    return false;
240 	        	};
241 
242 	        	//校验位的检测
243 	        	var checkParity = function (card)
244 	        	{
245 	        	    //15位转18位
246 	        	    card = changeFivteenToEighteen(card);
247 	        	    var len = card.length;
248 	        	    if (len == '18') {
249 	        	        var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
250 	        	        var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
251 	        	        var cardTemp = 0, i, valnum;
252 	        	        for (i = 0; i < 17; i++) {
253 	        	            cardTemp += card.substr(i, 1) * arrInt[i];
254 	        	        }
255 	        	        valnum = arrCh[cardTemp % 11];
256 	        	        if (valnum == card.substr(17, 1)) {
257 	        	            return true;
258 	        	        }
259 	        	        return false;
260 	        	    }
261 	        	    return false;
262 	        	};
263 
264 	        	//15位转18位身份证号
265 	        	var changeFivteenToEighteen = function (card)
266 	        	{
267 	        	    if (card.length == '15') {
268 	        	        var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
269 	        	        var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
270 	        	        var cardTemp = 0, i;
271 	        	        card = card.substr(0, 6) + '19' + card.substr(6, card.length - 6);
272 	        	        for (i = 0; i < 17; i++) {
273 	        	            cardTemp += card.substr(i, 1) * arrInt[i];
274 	        	        }
275 	        	        card += arrCh[cardTemp % 11];
276 	        	        return card;
277 	        	    }
278 	        	    return card;
279 	        	};
280 
281 	            //校验长度,类型
282 	            if (isCardNo(cardNumber) === false) {return false;}
283 	            //检查省份
284 	            if (checkProvince(cardNumber) === false) {return false;}
285 	            //校验生日
286 	            if (checkBirthday(cardNumber) === false) {return false;}
287 	            //检验位的检测
288 	            if (checkParity(cardNumber) === false) {return false;}
289 	        }
290 	        return true;
291 	    }
292 	};
293 
294 	return $we.string;
295 });