当前位置: 代码迷 >> JavaScript >> 惯用的js验证方法
  详细解决方案

惯用的js验证方法

热度:88   发布时间:2012-10-09 10:21:45.0
常用的js验证方法

function trim(s){ return s.replace(/(^\s*)|(\s*$)/g, "");}
function isEmail(s){ return s.search(/^\s*[\w\~\-\.]+\@[\w\~\-]+(\.[\w\~\-]+)+\s*$/g) >= 0;}
function isPassport(s) { return !(/[\'\"]/.test(s));}
function isMobile(s){ return (/^(13\d{9})|(15[8-9]\d{8})|(15[0|6]\d{8})$/.test(s));}
function isNumber(s){ return (/^\d+$/.test(s));}
function checkComma(s){ return (/,/.test(s));}
function chk_broswer(){ if (navigator.appName.indexOf("Microsoft")!=-1) { return(true); }else{ return(false); }}
function checkBrowser(){ if (navigator.appName.indexOf("Microsoft")!=-1) { return(true); }else{ return(false); }}
function checkKoreaChar(str) { for(var i=0; i<str.length; i++) { if(((str.charCodeAt(i) > 0x3130 && str.charCodeAt(i) < 0x318F) || (str.charCodeAt(i) >= 0xAC00 && str.charCodeAt(i) <= 0xD7A3))) { return true; } } return false;}
function checkAll(inputName, isChecked){ var boxes = document.getElementsByName(inputName); if(boxes){ for(var i = 0; i < boxes.length; i++){ if(boxes[i].disabled){ continue; } boxes[i].checked = isChecked ? true : false; } }}function selectAll(objSelect, isSelected){ if(objSelect.length){ for(var i = 0; i < objSelect.length; i++){ objSelect[i].selected = isSelected ? true : false; } }}
function checkBoxSelected(inputName){ var boxes = document.getElementsByName(inputName); if(boxes){ for(var i = 0; i < boxes.length; i++){ if(boxes[i].checked){ return true; } } } return false;}
function pickBoxSelected(pickBox){ if(pickBox){ for(var i = 0; i < pickBox.length; i++){ if(pickBox[i].selected){ return true; } } } return false;}
function checkMaxLength(textBox) { if (textBox.id) { var maxlength = document.getElementById(textBox.id).getAttribute("maxlength"); } if(maxlength){ if (textBox.value.length > maxlength){ textBox.value = textBox.value.substring(0, maxlength); } }}
function controlMaxLength(textBox) { var maxlength = document.getElementById(textBox.id).getAttribute("maxlength"); if(maxlength){ if (textBox.value.length > maxlength){ textBox.value = textBox.value.substring(0, maxlength); document.getElementById(textBox.id).scrollTop = document.getElementById(textBox.id).scrollHeight; } }}
function saveCurrentPos(textBox){ if (textBox.createTextRange) { textBox.currentPos = document.selection.createRange().duplicate(); }}
function setRadioValue(obj,value){ for(var i = 0;i<obj.length;i++){ if(obj[i].value == value){ obj[i].checked = true; break; } }}
function setValue(obj, value){ if(obj){ switch(obj.type){ case "text" : case "password" : case "file" : case "textarea" : case "hidden" : obj.value = value; break; case "select-one" : for(var i = 0;i<obj.length;i++){ if(obj.options[i].value == value){ obj.options[i].selected = true; break; } } break; case "radio" : for(var i = 0;i<obj.length;i++){ if(obj[i].value == value){ obj[i].checked = true; break; } } break; default : obj.value = value; break; } }}
function getRadioValue(radioName){ var boxes = document.getElementsByName(radioName); if(boxes){ for(var i = 0;i<boxes.length;i++){ if(boxes[i].checked){ return boxes[i].value; } } } return "";}
function resizeFrame(id){ var f = document.getElementById(id); var w = f.contentWindow; f.style.height = w.document.body.scrollHeight + "px";}
function hex2bin(hex){ var result = ""; if(hex && hex.length && hex.length % 2 === 0){ for(var i = 0 ;i<hex.length;i+=2){ result += "%"; result += hex.substr(i, 2); } result = decodeURIComponent(result); } return result;}
function bin2hex(bin){ var result = ""; var temp = ""; var i, chr; for(i=0;i<bin.length;i++){ chr = bin.charCodeAt(i); if(chr>127){ chr = encodeURIComponent(bin.charAt(i)); }else{ chr = chr.toString(16); if(chr.length == 1){ chr = "0" + chr; } } result += chr; } for(i=0;i<result.length;i++){ chr = result.charAt(i); if(chr!='%'){ temp+=chr; } } return temp.toLowerCase();}
function cnLength(str) { var escStr = escape(str); var numI = 0; var escStrlen = escStr.length; for (var i = 0; i < escStrlen; i++){ if(escStr.charAt(i) == '%'){ if(escStr.charAt(++i) == 'u'){ numI ++; } } } return str.length+numI;}
function setFocusPosition(obj, pos)
	{ if (obj.setSelectionRange) {
		setTimeout(function() { obj.setSelectionRange(pos, pos); obj.focus(); }, 0); 
	} else if (obj.createTextRange) {
		var rng = obj.createTextRange(); rng.collapse(true); rng.moveStart('character', pos); rng.moveEnd('character', pos); rng.select(); }
	}
	if (typeof (ImageLocator) == "undefined") {
		var ImageLocator = { getImageUrl : function (fileServer, fileName){
			var timestamp = fileName.split("_")[0].substr(0, 10);
			var d = new Date(timestamp * 1000);
			var month = (d.getMonth() + 1).toString();
			while (month.length < 2) { month = "0" + month; }
			var day = d.getDate().toString(); while (day.length < 2) { day = "0" + day; }
			var sec = Math.floor(timestamp % 200).toString();
			while (sec.length < 3) { sec = "0" + sec; }
			return "http://" + fileServer + "/" + d.getFullYear() + "/" + month + "/" + day + "/" + sec + "/" + fileName; }, 
			getThumbImageUrl : function (fileServer, fileName, extraName){ if (typeof (extraName) == "undefined") { extraName = ""; }
			var thumbFileName = "thumb" + extraName + "-" + fileName;
			var imageUrl = this.getImageUrl(fileServer, fileName);
			return imageUrl.replace(fileName, thumbFileName); }
		}}/*
?
  相关解决方案