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

常用的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 = escap