JS經常使用例子


// 是否爲空,非空返回真,不非爲空返回假
function isBlank(str) {
 var blankFlag = true;
 if (str.length == 0) return true;
 for (var i = 0; i < str.length; i++) {
  if ((str.charAt(i) != "") && (str.charAt(i) != " ")) {
   blankFlag = false;
   break;
  }
 }
 return blankFlag;
} url

function checkNotNull(theField, fieldName) {
 
 if(isBlank(theField.value)){
  alert(fieldName + "不可爲空!");
  theField.focus();
  return false;
 } spa

 return true;
} 字符串

// 是否爲數字
function checkNumber(theField, fieldName) {
  var pattern = /^([0-9]|(-[0-9]))[0-9]*((.[0-9]+)|([0-9]*))$/; it

  if(theField.value == "") return true;
  if (!pattern.test(theField.value)) {
   alert(fieldName + "必須爲合法數字");
   theField.focus();
   theField.select();
   return false;
  } io

 return true;
} function

// 是否爲指定範圍數字
function checkNumberRange(theField, fieldName, min, max) {
 if(theField.value == "") return true;
 if (!checkNumber(theField, fieldName)) return false; test

 if ((min != "") && (theField.value < min)) {
  alert(fieldName + "不可小於" + min + "!");
  theField.focus();
  theField.select();
  return false;
 } select

 if ((max != "") && (theField.value > max)) {
  alert(fieldName + "不可超過" + max + "!");
  theField.focus();
  theField.select();
  return false;
 } scroll

 return true;
} di

// 是否爲整數
function checkInteger(theField, fieldName) {
 var pattern = /^(d|(-d))d*$/;

 if(theField.value == "") return true;
 if (!pattern.test(theField.value)) {
  alert(fieldName + "必須爲整數!");
  theField.focus();
  theField.select();
  return false;
 }

 return true;
}

// 是否爲指定範圍內整數
function checkIntegerRange(theField, fieldName, min, max) {
 if(theField.value == "") return true;
 if (!checkInteger(theField, fieldName)) return false;

 if ((min != "") && (theField.value < min)) {
  alert(fieldName + "不可小於" + min + "!");
  theField.focus();
  theField.select();
  return false;
 }

 if ((max != "") && (theField.value > max)) {
  alert(fieldName + "不可超過" + max + "!");
  theField.focus();
  theField.select();
  return false;
 }

 return true;
}

// 是否爲正數
function checkPositiveNumber(theField, fieldName) {
 if(theField.value == "") return true;
 if (theField.value.charAt(0) == '-') {
  alert(fieldName + "必須爲正數!");
  theField.focus();
  return false;
 }

 return true;
}

// 限制字串最大長度
function checkLength(theField, fieldName, maxLength) {
 if(theField.value == "") return true;
 if (theField.value.length > maxLength) {
  alert(fieldName + "的字數最多爲" + maxLength + "字!");
  theField.select();
  theField.focus();
  return false;
 }

 return true;
}

// 限制字串長度,注意參數順序
function checkLength2(theField, fieldName, maxLength, minLength) {
 if(theField.value == "") return true;
 if (theField.value.length > maxLength) {
  alert(fieldName + "的字數最多爲" + maxLength + "字!");
  theField.focus();
  return false;
 }

 if ((minLength != "") && (theField.value.length < minLength)) {
  alert(fieldName + "的字數最少爲" + minLength + "字!");
  theField.focus();
  return false;
 }

 return true;
}

// 所輸入字符串是否均爲合法字符
// charBag中爲包含全部合法字符的字符串
function checkStrLegal(theField, fieldName, charBag) {
 if(theField.value == "") return true;
    for (var i = 0; i < theField.value.length; i++) {
        var c = theField.value.charAt(i);
        if (charBag.indexOf(c) == -1) {
       alert(fieldName + "含有非法字符(" + c + ")!");
       theField.focus();
       return false;
        }
    }

    return true;
}

// 所輸入字符串是否均爲合法字符
// charBag中爲包含非法字符的字符串
function checkStrLegal2(theField, fieldName, charBag) {
 if(theField.value == "") return true;
    for (var i = 0; i < theField.value.length; i++) {
        var c = theField.value.charAt(i);
        if (charBag.indexOf(c) > -1) {
       alert(fieldName + "含有非法字符(" + c +")!");
       theField.focus();
       return false;
        }
    }

    return true;
}

// 電子郵件驗證
function checkEmail(theField) {
 var pattern = /^.+@.+..+$/;

 if(theField.value == "") return true;
 if (!pattern.test(theField.value)) {
  alert("請輸入合法的電子郵件地址");
  theField.focus();
  theField.select();
  return false;
 }

 return true;
}

// 彈出一個窗口, 使用系統默認彈開位置 function popWindow(url, width, height, title){     window.open(url,"_blank","toolbar=no,location=no,directories=no, status=no,menubar=no,scrollbars=yes,resizable=yes,width="+width+ ",height="+height+",title="+title); }

  
相關文章
相關標籤/搜索