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

常用JS驗證
// JavaScript Document for public

//一些基本的驗證。2008-12-18 

//去除左右空格
String.prototype.trim = function () {
 return this.replace(/(^\s*)|(\s*$)/g, "");
};
//去左空格
String.prototype.ltrim = function () {
 return this.replace(/(^\s*)/g, "");
};  
//去右空格
String.prototype.rtrim = function () {
 return this.replace(/(\s*$)/g, "");
};

//驗證是否手機號碼
String.prototype.isMobile = function () {
 return (/^(?:13\d|15[89])-?\d{5}(\d{3}|\*{3})/.test(this.Trim()));
};


//驗證是否電話號碼
String.prototype.isTel = function () {
 return (/^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?/.test(this.Trim()));
};

//驗證是否郵箱
function isEmail(email) {
 var reg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
 return reg.test(email);
}
//驗證是否郵編
function isZip(zip) {
 var reg = /^\d{6}$/;
 return reg.test(zip);
}
//驗證是否數字
function isNumber(num) {
 return !isNaN(num);
}

//驗證是否是整數
function isInteger(str){
var regu = /^[-]{0,1}[0-9]{1,}$/;
return regu.test(str);
}

//驗證是否是正整數
function isPositiveInteger( str ){
 var regu = /^[0-9]{1,}$/;
 return regu.test(str);
}
function check(name) {
            var pattern=/[\/\\\:\*\?\"\<\>\|]/;
            
            if(name.search(pattern)!=-1) {
                alert("invalidate");
                return false;
            }
            return true;
        }
//驗證名稱是否含有非法字符
function isUnlawful(name){
 var reg = /[\/\\\:\*\?\"\<\>\|]/;
 return reg.test(name);
}

//驗證名稱是否含有中文
function isCN(name){
    var fn = get_fileName(name);
 var reg = /^[A-Za-z0-9]+$/;
 return reg.test(fn);
}

/////////////////////////////////////////////////////////////////////////////////表單驗證
//驗證年齡
function checkAge(age) {
 var age = document.getElementById("age").value.trim();
 if (!isNumber(age)) {
  //alert("請輸入正確的年齡,須為數字");
  return false;
 }
 if (age < 5 || age > 200) {
  //alert("年齡須在(5-200)之間");
  return false;
 }
 return true;
}

//驗證郵箱
function checkEmail(email) {
 var email = document.getElementById("email").value.trim();
 if (!isEmail(email)) {
  //alert("請輸入正確的郵箱格式,如:yangshuo@163.com");
  return false;
 }
 return true;
}

//驗證郵編
function checkZip(zip) {
 var zip = document.getElementById("zip").value.trim();
 if (!isZip(zip)) {
  //alert("請輸入正確的郵編,須為6位數字");
  return false;
 }
 return true;
}

//驗證手機號碼
function checkMobile(mobile) {
 var mobile = document.getElementById("mobile").value.trim();
 if (!mobile.isMobile()) {
  alert("\u8acb\u8f38\u5165\u6b63\u78ba\u7684\u624b\u6a5f\u865f\u78bc\uff0c\u9808\u70ba11\u4f4d\u6578\u5b57\uff01");
  return false;
 }
 return true;
}

//驗證電話號碼
function checkTel(tel) {
 var tel = document.getElementById("tel").value.trim();
 if (!tel.isTel()) {
  alert("\u8acb\u8f38\u5165\u6b63\u78ba\u7684\u96fb\u8a71\u865f\u78bc\u683c\u5f0f\uff0c\u5982\uff1a021-8888888");
  return false;
 }
 return true;
}

//驗證身份證號碼 copy
//20080314 ivor add
function chkIDdata(obj){
  var sid = obj.value.trim();
  //alert("chkdata sid : "+sid);
  var alphabet = new Array(36), little = new Array(36);
            var number = new Array(10);
            alphabet[10] = 'A'; little[10] = 'a';
            alphabet[11] = 'B'; little[11] = 'b';
            alphabet[12] = 'C'; little[12] = 'c';
            alphabet[13] = 'D'; little[13] = 'd';
            alphabet[14] = 'E'; little[14] = 'e';
            alphabet[15] = 'F'; little[15] = 'f';
            alphabet[16] = 'G'; little[16] = 'g';
            alphabet[17] = 'H'; little[17] = 'h';
            alphabet[18] = 'J'; little[18] = 'j';
            alphabet[19] = 'K'; little[19] = 'k';
            alphabet[20] = 'L'; little[20] = 'l';
            alphabet[21] = 'M'; little[21] = 'm';
            alphabet[22] = 'N'; little[22] = 'n';
            alphabet[23] = 'P'; little[23] = 'p';
            alphabet[24] = 'Q'; little[24] = 'q';
            alphabet[25] = 'R'; little[25] = 'r';
            alphabet[26] = 'S'; little[26] = 's';
            alphabet[27] = 'T'; little[27] = 't';
            alphabet[28] = 'U'; little[28] = 'u';