⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utilityjs.inc

📁 一个用php+mysql做的图书馆管理系统
💻 INC
字号:
<SCRIPT LANGUAGE="JavaScript">
cSPACE = " ";
gFieldBeingChecked = "";

function Collapse(aString, theCh)
{
var newString = "";
for (var i=0; i < aString.length; i++){
var ch = aString.substring(i, i+1);
if (ch != theCh){
newString += ch;
}
}
return newString;
}

function CheckSelectVal(formname, fieldname, alertVal, withAlert, minval, maxval) {
   var myval = getselectval(formname, fieldname);
   var retval = true;
   if (myval < minval || myval > maxval) {
      if (withAlert) {
         alert(alertVal);
      }
      return false;
   }
   return retval;
}

function getselectval(formname, fieldname) {
// this function is not necessary for IE, but is for NN
//   it returns the value of the selected option, given a select object.
//   IE automatically assigns the value of the selected option to the .value property
//    of the select object.

  var obj = new Object;
  var i;
  var retval;
  
  obj = eval("document." + formname + "." + fieldname);
  retval = -1;  
  for (i=0; i < obj.options.length; i++) {
    if (obj.options[i].selected == true) {
     retval = obj.options[i].value;
     break;
   }
  }
  return retval;
}

function setselectval(formname, fieldname, val) {
// this function sets the value of the selected option, given a select object and
//  a value--if no value in the select list matches the passed value, it does nada.

  var obj = new Object;
  var i;
  var retval=false;
  obj = eval("document." + formname + "." + fieldname);
  for (i=0; i < obj.options.length; i++) {
    if (obj.options[i].value == val) {
     obj.options[i].selected = true;
     retval = true;
     break;
   }
  }
  return retval;
}

function CheckNumberWithMin(theField, fieldName, withAlert, fieldRequired, withNULL, minval) {
   if (CheckNumber(theField, fieldName, withAlert, fieldRequired, withNULL) == false) return false;
   if (theField.value < minval) {
      if (withAlert) alert('Invalid ' + fieldName + ' entry.');
      theField.focus();
      theField.select();
      return false;
   }
   return true;
}

function CheckNumber(theField, fieldName, withAlert, fieldRequired, withNULL)
{
if (( withNULL == true ) && (theField.value == "" || theField.value == null)) return false;
var testString = Collapse(theField.value, cSPACE);
if ( IsNumber(testString, fieldName, withAlert, fieldRequired) == false){
theField.focus();
theField.select();
return false;
}
return true;
}

function IsNotBlank(testString, fieldName, withAlert)
{
// Check if the Field is blank
var stringLen = testString.length;
var returnCode = false;
var i = 0;
if (stringLen == 0 || testString == "" || testString == null ){
if (withAlert) {
alert( "Sorry, but the " + fieldName + " field requires an entry.");
}
return false;
}
for (i=0; i < stringLen; i++){
var ch = testString.substring(i, i+1);
if (ch != " "){
returnCode = true;
break;
}
}
if (returnCode == false ) {
if (withAlert){
alert( "Sorry, but the " + fieldName + " field requires an entry.");
}
return false;
} else {
return true;
}
}

function IsNumber(testString, fieldName, withAlert, fieldRequired)
{
   var stringLen = testString.length;
   var i = 0;
   if (fieldRequired == false ){
      if (IsNotBlank(testString, fieldName, false) == false ){
      return true;
      }
   }
   else {
      if (IsNotBlank(testString, fieldName, withAlert) == false ){
      return false;
      }
   }
   var numperiods = 0; 
   for (i=0; i < stringLen; i++){
     var ch = testString.substring(i, i+1);
   // isNaN should not be used here.
     if ( (ch < "0" || ch > "9") && ch != "." && ch != "-" ) {
       if (withAlert) {
        if (ch == ",") {
          alert("A comma is not necessary to separate thousands in the " + fieldName + " field.  For example, write 12,000 as 12000.")
        }
        else {
           alert( "Sorry, but the " + fieldName + " field takes only numeric characters (0 to 9).  It will not accept letters or punctuation.");
        }
       }
       return false;

     }
     else {
       if (ch == ".") numperiods++;
      if (numperiods > 1) {
        if (withAlert) {
          alert("Sorry, only one decimal point is allowed in the " + fieldName + " field.");
        }
        return false;
      }
      if ( (ch == "-") && (i > 0) ) {
        if (withAlert) {
          alert("Sorry, a '-' character is only allowed to indicate a negative number.");
        }
        return false;
      }
     }
   }
   return true;
}

function IsDate(testString, fieldName, withAlert, fieldRequired)
{
   var stringLen = testString.length;
   var i = 0;
   if (fieldRequired == false) {
      if (IsNotBlank(testString, fieldName, false) == false) {
         return true;
      }
   }
   else {
      if (IsNotBlank(testString, fieldName, withAlert) == false) {
         return false;
      }
   }
   myDate = Date.parse(testString)
   if (isNaN(myDate)){
      if (withAlert) {
         alert( "Sorry, but the " + fieldName + " field does not recognize that date format.  Please enter dates as mm/dd/yyyy.");
      }
      return false;
   }
   else {

     MonthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
     var thisDate = new Date(myDate);
     var nowDate = new Date();
//   alert(testString);
//   alert(" " + thisDate.getMonth() + "/" + thisDate.getDate() + "/" + thisDate.getFullYear());
     var retval = new Boolean();
     retval = true;
     var delim1 = testString.indexOf("/");
     var delim2 = testString.indexOf("/", delim1+1);
     var numMon = parseInt(testString.substring(0, delim1), 10);
     var numDate = parseInt(testString.substring(delim1 + 1, delim2), 10);
     var numYr = parseInt(testString.substr(delim2+1), 10);
//   alert (numYr);
//   alert(" " + numMon + "/" + numDate + "/" + numYr);
     if (numYr < 100) { numYr +=2000; }
     setLeap(numYr);
//   alert (numYr);
     if (numMon < 1 || numMon > 12) {
       if (withAlert) {
         alert("Invalid month: " + numMon);
      }
      retval = false; }
     if (numDate < 1 || numDate > MonthDays[numMon-1]) {
       if (withAlert) {
         alert("Invalid date: " + numDate);
      }
      retval = false; }
     var thisYear = nowDate.getFullYear();
     if (numYr < thisYear) {
       if (withAlert) {
         alert("Invalid year: " + numYr);
      }
      retval = false; }
     if ((numYr - thisYear) > 1) {
       if (withAlert) {
         alert("Please verify year: " + numYr);
      }}
     return retval;
   }
}

  function setLeap(CurYear) {
// account for leap years--must redo every time year is changed, hence it's a function
   if (((CurYear % 4)==0) && ((CurYear % 100)!=0) || ((CurYear % 400)==0)) {
     MonthDays[1] = 29;
   }
   else {
     MonthDays[1] = 28;
   }
  }



function CheckDate(textcontrol, fieldName, withAlert, fieldRequired, withNULL)
{
   if (gFieldBeingChecked != "" && gFieldBeingChecked!=fieldName) {
      return;
   }
   gFieldBeingChecked=fieldName;
   
   if (( withNULL == true ) && (textcontrol.value == "" || textcontrol.value == null)) return false;
   
   var testString = Collapse(textcontrol.value, cSPACE);
   if (fieldRequired == false ){
      if (IsNotBlank(testString, fieldName, false) == false ){
         gFieldBeingChecked="";
         return true;
      }
   }
   else {
      if (IsNotBlank(testString, fieldName, withAlert) == false ){
         textcontrol.focus();
         textcontrol.select();
         return false;
      }
   }
   if (IsDate(testString, fieldName, withAlert, fieldRequired) == false){
      textcontrol.focus();
      textcontrol.select();
      return false;
   }
   gFieldBeingChecked="";
   return true;
}

function Checkhhmm(theField, fieldName, withAlert, fieldRequired, withNULL)
{
   if (gFieldBeingChecked != "" && gFieldBeingChecked!=fieldName) {
      return;
   }
   gFieldBeingChecked=fieldName;

   if (( withNULL == true ) && (theField.value == "" || theField.value == null)) return false;
   var testString = Collapse(theField.value, cSPACE);
   var theSpot = testString.indexOf(':')
   if (theSpot < 0 ){
      alert ("Sorry, but the " + fieldName + " field must have a colon(:).  Please enter as hh:mm.");
         theField.focus();
         theField.select();
      return false;
   }
   if (theSpot > 0){
      if ( IsNumber(testString.substring(0,theSpot), fieldName, false, false) == false){
         alert ("Sorry, but the hour in the " + fieldName + " field must be numeric.  Please enter as hh:mm.");
         theField.focus();
         theField.select();
         return false;
      }
      else {
        var numHr = parseInt(testString.substring(0,theSpot));
        if (numHr < 0 || numHr > 23) {
          alert ("Invalid hour: " + numHr);
         theField.focus();
         theField.select();
         return false;
        }
      }
   }
   if (theSpot < testString.length){
      if ( IsNumber(testString.substring(theSpot+1,testString.length), fieldName, false, false) == false){
         alert ("Sorry, but the minute in the " + fieldName + " field must be numeric.  Please enter as hh:mm.");
         theField.focus();
         theField.select();
         return false;
      }
      else {
        var numMin = parseInt(testString.substring(theSpot+1,testString.length));
        if (numMin < 0 || numMin > 59) {
          alert("Invalid minute: " + numMin);
         theField.focus();
         theField.select();
         return false;
        }
      }
   }
   gFieldBeingChecked="";
   return true;
}

function CheckOnOff(theField, fieldName, withAlert, fieldRequired, withNULL)
{
   if (gFieldBeingChecked != "" && gFieldBeingChecked!=fieldName) {
      return;
   }
   gFieldBeingChecked=fieldName;

   if (( withNULL == true ) && (theField.value == "" || theField.value == null)) return false;
   var testString = Collapse(theField.value, cSPACE);
   if (fieldRequired == false ){
      if (IsNotBlank(testString, fieldName, false) == false ){
         gFieldBeingChecked="";
         return true;
      }
   }
   else {
      if (IsNotBlank(testString, fieldName, withAlert) == false ){
         return false;
      }
   }
   if ((testString.toLowerCase() == "on") || (testString.toLowerCase() == "off")){
      gFieldBeingChecked="";
      return true;
   }
   else {
      alert ("Sorry, but the " + fieldName + " field may have only the values On or Off.");
      textcontrol.focus();
      textcontrol.select();
      return false;
   }
}
</script>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -