日期:2014-05-16  浏览次数:20316 次

javascript 工具类 trim 数据验证等
function ByteString() {
	//判断是不是为正整数
	this.checkThanZero = function(str) {
		if (/^[0-9]*[1-9][0-9]*$/.test(str))
			return true;
		else
			return false;
	};
	// 对字符串进行trim
	this.trim = function(str) {
		return str.replace(/(^\s*)|(\s*$)/g, "");
	};
	/**
	 * 判断字符状态,如果字符是数字返回1,大写字母返回2,小写字母返回3,特殊字符返回4
	 */
	this.charMode = function(iN) {
		if (iN >= 48 && iN <= 57) // 数字
			return 1;
		if (iN >= 65 && iN <= 90) // 大写字母
			return 2;
		if (iN >= 97 && iN <= 122) // 小写
			return 3;
		else
			return 4; // 特殊字符
	};
	/**
	 * 计算字符串的字符数,注:一个中文为两个字符
	 */
	this.getStrLen = function(Obj) {
		var nCNLenth = 0;
		var nLenth = Obj.length;
		for ( var i = 0; i < nLenth; i++) {
			if (Obj.charCodeAt(i) > 255) {
				nCNLenth += 2;
			} else {
				nCNLenth++;
			}
		}
		return nCNLenth;
	};
	/**
	 * 判断是不是为数字,如果mun,max都为数字,num <= max,并且num > 0返回TRUE,否则返回FALSE
	 */
	this.checkNumber = function(num, max) {
		if (/^[0-9]+$/.test(num) && num <= max && num > 0)
			return true;
		else
			return false;
	};
	// 判断是否为数字和字母组合的一种或者两种 如果出现除数字字母外的其余字符返回false 否则返回true
	this.checkEnglishAndNumber = function(str) {
		if (/^[A-Za-z0-9]+$/.test(str))
			return true;
		else
			return false;
	};
	/**
	 * 是否为联通G网用户 是返回 true 否则返回 false
	 */
	this.isGNet = function(str) {
		if (str.length != 11)
			return false;
		if (/^13[0-2][0-9]{8}|15[5-6][0-9]{8}|18[6][0-9]{8}$/.test(str))
			return true;
		else
			return false;
	};
	// 判断是字符串是不是整数,是返回true 否则返回false
	this.checkInteger = function(str) {
		if (/^-?\d+$/.test(str))
			return true;
		else
			return false;
	};
	/**
	 * 判断是否为1901(含1901)到现在的某一年,是返回TRUE,否则返回FALSE
	 */
	this.checkYear = function(year) {
		if (year == "")
			return false;
		if (!this.checkNumber(year, new Date().getFullYear())
				|| parseInt(year) < 1901)
			return false;
		return true;
	};
	/**
	 * 判断是否为1-12的某一月,是返回TRUE,否则返回FALSE
	 */
	this.checkMonth = function(month) {
		if (month == "")
			return false;
		if (!this.checkNumber(month, 12))
			return false;
		return true;
	};
	/**
	 * 检查从1901 到今年的某天是否存在(如果是今年的某天不一定已经过去) 存在返回FALSE 不存在返回TRUE
	 */
	this.checkDay = function(year, month, day) {
		if (!this.checkYear(year)) {
			return false;
		}
		if (!this.checkMonth(month)) {
			return false;
		}
		var maxday = 31;
		switch (parseInt(month)) {
		case 1:
			maxday = 31
			break
		case 2:
			if (isLeapYear(year))
				maxday = 29
			else
				maxday = 28
			break
		case 3:
			maxday = 31
			break
		case 4:
			maxday = 30
			break
		case 5:
			maxday = 31
			break
		case 6:
			maxday = 30
			break
		case 7:
			maxday = 31
			break
		case 8:
			maxday = 31
			break
		case 9:
			maxday = 30
			break
		case 10:
			maxday = 31
			break
		case 11:
			maxday = 30
			break
		case 12:
			maxday = 31
			break
		default:
			maxday = 31;
		}
		if (!this.checkNumber(day, maxday))
			return false;
		return true;
	};
	/**
	 * 判断是否为手机号码
	 */
	this.checkMobile = function(mobile) {
		if (mobile == "")
			return false;
		if (/^13\d{9}$/.test(mobile) | /^15\d{9}$/.test(mobile)
				| /^18\d{9}$/.test(mobile))
			return true;
		return false;
	};
	/**
	 * 判断是否为G网手机,即联通非CDMA手机号码
	 */
	this.isGNet = function(mobile) {
		if (mobile == "")
			return false;
		if (/^13[0-2][0-9]{8}|15[5-6][0-9]{8}|18[6][0-9]{8}$/.test(mobile))
			return true;
		return false;
	};
	/**
	 * 判断是否为C网手机,CDMA手机号码
	 */
	this.isCNet = function(mobile) {
		if (mobile == "")
			return false;
		if (/^13[3][0-9]{8}|15[3][0-9]{8}|18[9][0-9]{8}$/.test(mobile))
			return true;
		return false;
	};
	/**
	 * 判断是否为移动号码
	 */
	this.isMobileNet = function(mobile) {
		if (mobile == "")
			return false;
		if (/^13[4-9][0-9]{8}|15[01289][0-9]{8}$/.test(mobile))
			return true;
		return false;
	};
	// 判断闰年
	this.isLeapYear = function(year) {
		return (0 == year % 4 && ((year % 100 != 0) || (year % 400 == 0)));
	};
	//使Id为objId的下拉框中option值为optionVal的项选中,如果没有这一项,不对select进行任何操作
	this.makeO