よくあるjsの検証


js脚本を書く時、いろいろな検証関数を書きます.ここで省の所在地を整理して探します.

/**
 *         
 * @param value
 */
function isValidEmail(value) {
	var sReg = /[_a-zA-Z\d\-\.]+@[_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+)+$/;
	if (!sReg.test(value)) {
		return false;
	}
	return true;   
}

/**
 *           
 * @param value
 * @returns {Boolean}
 */
function isNotEmpty(value) {
	if (null == value || "" == value) {
		return false;
	} else {
		return true;
	}
}

/**
 *           
 * @param value
 * @returns {Boolean}
 */
function isEmpty(value) {
	if (null == value || "" == value) {
		return true;
	} else {
		return false;
	}
}

/**
 *         
 * @param val    
 * @param maxLength     
 * @returns {Boolean}
 */
function checkLength(val, maxLength) {
	var valLength = 0;
	for (var i = 0; i < val.length; i++) {
		var a = val.charAt(i);
		if (a.match(/[^\x00-\xff]/ig) != null) {
			valLength += 2;
		} else {
			valLength += 1;
		}
	}
	if (valLength > maxLength) {
		return false;
	} else {
		return true;
	}
}

/**
 *        ,  true=    ,false=   
 * @param object
 * @returns (Boolean)
 */
function isIdCard(object) {
	var partten = /^[\d]{6}((19[\d]{2})|(200[0-8]))((0[1-9])|(1[0-2]))((0[1-9])|([12][\d])|(3[01]))[\d]{3}[0-9xX]$/;
	var idnumber = object;
	if (partten.test(idnumber)) {
		var truenum = idnumber.substring(0, 17);
		var yzm;

		nsum = truenum.substring(0, 1) * 7;
		nsum = nsum + truenum.substring(1, 2) * 9;
		nsum = nsum + truenum.substring(2, 3) * 10;
		nsum = nsum + truenum.substring(3, 4) * 5;
		nsum = nsum + truenum.substring(4, 5) * 8;
		nsum = nsum + truenum.substring(5, 6) * 4;
		nsum = nsum + truenum.substring(6, 7) * 2;
		nsum = nsum + truenum.substring(7, 8) * 1;
		nsum = nsum + truenum.substring(8, 9) * 6;
		nsum = nsum + truenum.substring(9, 10) * 3;
		nsum = nsum + truenum.substring(10, 11) * 7;
		nsum = nsum + truenum.substring(11, 12) * 9;
		nsum = nsum + truenum.substring(12, 13) * 10;
		nsum = nsum + truenum.substring(13, 14) * 5;
		nsum = nsum + truenum.substring(14, 15) * 8;
		nsum = nsum + truenum.substring(15, 16) * 4;
		nsum = nsum + truenum.substring(16, 17) * 2;
		yzm = 12 - nsum % 11;
		if (yzm == 10) {
			yzm = 'x';
		} else if (yzm == 12) {
			yzm = '1';
		} else if (yzm == 11) {
			yzm = '0';
		}
		if (idnumber.substring(17, 18).toLowerCase() == yzm) {
			return true;
		}
		else{
			return false;
		}
	} 
}

/**
 *      /   ,          
 * @param enName
 * @returns (Boolean)
 */
function isEnName(enName){
	var checkENName=/^[A-Za-z ]+$/;   
	if(checkENName.test(enName)){
		return true;
	}
	else{
		return false;
	}
}


/**
 *          
 * @param cnStr
 * @returns (Boolean)
 */
function isCn(cnStr){
	if(cnStr.length == 0){
		return false;
	}
	var checkChinese=/^([\u4E00-\u9FA5])*$/;
	if(checkChinese.test(cnStr)){							
		return true;
	}
	else{
		return false;
	}
}

/**
 *          
 * @param str
 * @returns tmp
 */
function ToCDB(str) { 
    var tmp = ""; 
    for (var i = 0; i < str.length; i++) { 
        if (str.charCodeAt(i) > 65248 && str.charCodeAt(i) < 65375) { 
            tmp += String.fromCharCode(str.charCodeAt(i) - 65248); 
        } 
        else { 
            tmp += String.fromCharCode(str.charCodeAt(i)); 
        } 
    } 
    return tmp;
}

/**
 *          
 * @param value
 */
function isValidMobile(value) {
	//          
	if (isNaN(value)) {
		return false;
	}
	//      11 
	if (value.length != 11) {
		return false;
	}
	return true;
}
function isMobilePhone(phoneNo) {
	var regMobile=/^0?1(3|4|5|8)\d{9}$/;
	if(regMobile.test(phoneNo)) {
		return true;
	}
	else {
		return false;
	}
}