JS各种常用条件判断代码集合

JS各种常用条件判断代码集合

//去掉空格
function Trim(str){
if(str.charAt(0) == " "){powered by 25175.net
  str = str.slice(1);
  str = Trim(str);
}
return str;
}

//判断是否是空
function isEmpty(pObj,errMsg){
var obj = eval(pObj);
if( obj == null || Trim(obj.value) == ""){
  if (errMsg == null || errMsg =="")
   alert("输入为空!");
  else
   alert(errMsg);
  obj.focus();
  return false;
}
return true;
}

//判断是否是数字
function isNumber(pObj,errMsg){
var obj = eval(pObj);
strRef = "1234567890";
if(!isEmpty(pObj,errMsg))return false;
for (i=0;i<obj.value.length;i++) {
  tempChar= obj.value.substring(i,i+1);
  if (strRef.indexOf(tempChar,0)==-1) {
   if (errMsg == null || errMsg =="")
    alert("数据不符合要求,请检查");
   else
    alert(errMsg);
   if(obj.type=="text")
    obj.focus();
   return false;
  }
}
return true;
}

//判断是否是数字,数字可以为负数
function isNegative(pObj,errMsg){
var obj = eval(pObj);
strRef = "1234567890-";
if(!isEmpty(pObj,errMsg))return false;
for (i=0;i<obj.value.length;i++) {
  tempChar= obj.value.substring(i,i+1);
  if (strRef.indexOf(tempChar,0)==-1) {
   if (errMsg == null || errMsg =="")
    alert("数据不符合要求,请检查");
   else
    alert(errMsg);
   if(obj.type=="text")
    obj.focus();
   return false;
  }else{
   if(i>0){
    if(obj.value.substring(i,i+1)=="-"){
     if (errMsg == null || errMsg =="")
      alert("数据不符合要求,请检查");
     else
      alert(errMsg);  
     if(obj.type=="text")
     obj.focus();
     return false;
    }
   }
  }
}
return true;
}

//判断是否是钱的形式
function isMoney(pObj,errMsg){
  var obj = eval(pObj);
  strRef = "1234567890.";
  if(!isEmpty(pObj,errMsg)) return false;
    for (i=0;i<obj.value.length;i++) {
     tempChar= obj.value.substring(i,i+1);
     if (strRef.indexOf(tempChar,0)==-1) {
      if (errMsg == null || errMsg =="")
    alert("数据不符合要求,请检查");
      else
    alert(errMsg);  
    if(obj.type=="text")
    obj.focus();
    return false;
    }
    else{
    tempLen=obj.value.indexOf(".");
    if(tempLen!=-1){
      strLen=obj.value.substring(tempLen+1,obj.value.length);
      if(strLen.length>2){
        if (errMsg == null || errMsg =="")
        alert("数据不符合要求,请检查");
        else
        alert(errMsg);  
        if(obj.type=="text")
          obj.focus();
          return false;
       }
      }
     }
  }
  return true;
}

function isLeapYear(year)
{
if((year%4==0&&year%100!=0)||(year%400==0))
{
return true;
}  
return false;
}

//判断时间是否正确

function isDate(checktext){
var datetime;
var year,month,day;
var gone,gtwo;
if(Trim(checktext.value)!=""){
datetime=Trim(checktext.value);
if(datetime.length==10){
  year=datetime.substring(0,4);
  if(isNaN(year)==true){
   alert("请输入日期!格式为(yyyy-mm-dd) \n例(2001-01-01)!");
   checktext.focus();
   return false;
  }
  gone=datetime.substring(4,5);
  month=datetime.substring(5,7);
  if(isNaN(month)==true){
   alert("请输入日期!格式为(yyyy-mm-dd) \n例(2001-01-01)!");
   checktext.focus();
   return false;
  }
  gtwo=datetime.substring(7,8);
  day=datetime.substring(8,10);
  if(isNaN(day)==true){
   alert("请输入日期!格式为(yyyy-mm-dd) \n例(2001-01-01)!");
   checktext.focus();
   return false;
  }
  if((gone=="-")&&(gtwo=="-")){
   if(month<1||month>12) {
    alert("月份必须在01和12之间!");
    checktext.focus();
    return false;
    }
   if(day<1||day>31){
    alert("日期必须在01和31之间!");
    checktext.focus();
    return false;
   }else{
    if(month==2){  
     if(isLeapYear(year)&&day>29){
       alert("二月份日期必须在01到29之间!");
       checktext.focus();
       return false;
     }      
     if(!isLeapYear(year)&&day>28){
       alert("二月份日期必须在01到28之间!");
       checktext.focus();
       return false;
     }
    }
    if((month==4||month==6||month==9||month==11)&&(day>30)){
     alert("在四,六,九,十一月份 \n日期必须在01到30之间!");
     checktext.focus();
     return false;
    }
   }
  }else{
   alert("请输入日期!格式为(yyyy-mm-dd) \n例(2001-01-01)");
   checktext.focus();
   return false;
  }
}else{
  alert("请输入日期!格式为(yyyy-mm-dd) \n例(2001-01-01)");
  checktext.focus();
  return false;
}
}else{
return true;
}
return true;
}


//判断是否月份正确

function isYearMonth(checktext){
  var datetime;
  var year,month,day;
  var gone,gtwo;
  if(Trim(checktext.value)!=""){
    datetime=Trim(checktext.value);
    if(datetime.length==7){
    year=datetime.substring(0,4);
    if(isNaN(year)==true){
     alert("输入错误!格式为(yyyy-mm) \n例(2001-01)!");
     checktext.focus();
     return false;
    }
    gone=datetime.substring(4,5);
    month=datetime.substring(5,7);
    if(isNaN(month)==true){
     alert("输入错误!格式为(yyyy-mm) \n例(2001-01)!");
     checktext.focus();
     return false;
    }
    if((gone=="-")){
      if(month<1||month>12) {
       alert("月份必须在01和12之间!");
       checktext.focus();
       return false;
       }
    }
    else{
      alert("输入错误!格式为(yyyy-mm) \n例(2001-01)!");
      checktext.focus();
      return false;  
    }
   }
    else{
      alert("输入错误!格式为(yyyy-mm) \n例(2001-01)!");
      checktext.focus();
      return false;
    }
}
  return true;
}

function fucCheckNUM(NUM) {
  if (NUM.length!=11)
   return false;
    
  var i,j,strTemp;
  strTemp="0123456789";
  if ( NUM.length== 0)
   return false;
  if ( NUM.length== undefined||NUM==undefined)
   return false;
  for (i=0;i<NUM.length;i++)
  {
   j=strTemp.indexOf(NUM.charAt(i));
   if (j==-1)
   {
   //说明有字符不是数字
   return false;
   }
  }
  //说明是数字
  return true;
}
  
function click () {
  //alert ('不许偷看!禁止使用鼠标右键!');
  window.event.returnValue=false;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,那我们可以用一个名为 "util" 的对象来封装这些函数,例如: ``` const util = { add: function(a, b) { return a + b; }, subtract: function(a, b) { return a - b; }, multiply: function(a, b) { return a * b; }, divide: function(a, b) { return a / b; } }; ``` 然后你就可以通过访问 "util.add(a, b)" 来使用加法函数了。你也可以继续往这个对象里添加其他常用的函数。 需要注意的是,以上代码假设你使用的是 ES6 语法,如果你使用的是 ES5 语法,你需要把 "function" 换成 "var"。 ### 回答2: 封装一个常用的JavaScript函数集合是为了提高开发效率和代码的重用性。这个函数集合可以包含各种常用的功能函数,例如: 1. 获取URL参数值:可以封装一个函数,通过传入参数名,返回对应的URL参数值。比如,可以使用window.location.search获取URL参数字符串,然后再使用正则表达式或其他方法找到对应的参数值。 2. 时间格式化:封装一个函数,将时间对象转换为指定格式的字符串。比如,可以使用Date对象的方法获取年月日、时分秒等信息,然后根据需要进行格式化输出。 3. 随机数生成:可以封装一个函数,生成指定范围内的随机整数或随机浮点数。比如,可以使用Math.random函数生成0到1之间的随机数,然后根据需要进行相应的运算转换。 4. 数组元素查找:可以封装一个函数,根据指定条件在数组中查找元素,并返回符合条件的元素或索引值。比如可以使用Array的filter方法根据判定函数找到满足条件的元素。 5. 数据类型判断:可以封装一个函数,判断变量的数据类型并返回相应的字符串表示。比如,可以使用typeof运算符判断基本数据类型,使用Array.isArray判断数组类型,使用Object.prototype.toString判断复杂数据类型。 6. 字符串截取:可以封装一个函数,对字符串进行截取操作,根据指定参数截取指定长度的字符串,同时提供省略号等处理方式。 7. 表单验证:可以封装一些常用的表单验证函数,例如验证邮箱、手机号码、用户名等。这些函数可以使用正则表达式或其他验证方式进行验证,以提高代码的可读性和可维护性。 以上只是一些常见的封装函数示例,实际的常用函数集合可以根据具体需求进行扩展和定制。 ### 回答3: 常用的JavaScript函数集合可以提供一组常见功能的封装,以便在开发过程中更便捷地调用这些函数。下面是一个封装了一些常用功能的常用JavaScript函数集合例子: 1. 获取URL参数:这个函数可以从URL中获取指定参数的值并返回。可以通过传递参数名来获取不同的参数值。 2. 格式化日期:这个函数可以将日期对象或日期字符串格式化为指定的日期格式。可以通过参数来指定所需的日期格式。 3. 判断空值:这个函数可以判断一个变量是否为空(包括未定义、null、空字符串等),返回true或false。 4. 随机数生成:这个函数可以生成一定范围内的随机数,可以通过传递参数指定范围。 5. 数组去重:这个函数可以对数组进行去重操作,并返回去重后的数组。 6. 节流函数:这个函数可以控制函数的频率,在指定的时间间隔内只执行一次。 7. 深度拷贝对象:这个函数可以实现深度拷贝一个对象,返回一个拷贝后的新对象。 8. 字符串截取:这个函数可以截取指定长度的字符串,并在结尾添加省略号。 9. 图片预加载:这个函数可以实现图片的预加载,防止图片未加载完全时导致页面闪烁。 10. 防抖函数:这个函数可以控制函数的执行频率,在指定的时间间隔内,只有最后一次触发才执行。 这些是常用的JavaScript函数集合的一些例子,通过封装这些功能,可以使开发过程更高效、便捷。可以根据需要自定义封装适合自己的常用功能函数集合

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值