栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

常见JS验证脚本汇总

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

常见JS验证脚本汇总

本文实例讲述了常见JS验证脚本。分享给大家供大家参考,具体如下:


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, "");
}
function checkIsNotEmpty(str)
{
  if(str.trim() == "")
    return false;
  else
    return true;
}//~~~
function checkIsInteger(str)
{
  //如果为空,则通过校验
  if(str == "")
    return true;
  if(/^(\-?)(\d+)$/.test(str))
    return true;
  else
    return false;
}//~~~
function checkIntegerMinValue(str,val)
{
  //如果为空,则通过校验
  if(str == "")
    return true;
  if(typeof(val) != "string")
    val = val + "";
  if(checkIsInteger(str) == true)
  {
    if(parseInt(str,10)>=parseInt(val,10))
      return true;
    else
      return false;
  }
  else
    return false;
}//~~~
function checkIntegerMaxValue(str,val)
{
  //如果为空,则通过校验
  if(str == "")
    return true;
  if(typeof(val) != "string")
    val = val + "";
  if(checkIsInteger(str) == true)
  {
    if(parseInt(str,10)<=parseInt(val,10))
      return true;
    else
      return false;
  }
  else
    return false;
}//~~~
function isNotNegativeInteger(str)
{
  //如果为空,则通过校验
  if(str == "")
    return true;
  if(checkIsInteger(str) == true)
  {
    if(parseInt(str,10) < 0)
      return false;
    else
      return true;
  }
  else
    return false;
}//~~~
function checkIsDouble(str)
{
  //如果为空,则通过校验
  if(str == "")
    return true;
  //如果是整数,则校验整数的有效性
  if(str.indexOf(".") == -1)
  {
    if(checkIsInteger(str) == true)
      return true;
    else
      return false;
  }
  else
  {
    if(/^(\-?)(\d+)(.{1})(\d+)$/g.test(str))
      return true;
    else
      return false;
  }
}//~~~
function checkDoubleMinValue(str,val)
{
  //如果为空,则通过校验
  if(str == "")
    return true;
  if(typeof(val) != "string")
    val = val + "";
  if(checkIsDouble(str) == true)
  {
    if(parseFloat(str)>=parseFloat(val))
      return true;
    else
      return false;
  }
  else
    return false;
}//~~~
function checkDoubleMaxValue(str,val)
{
  //如果为空,则通过校验
  if(str == "")
    return true;
  if(typeof(val) != "string")
    val = val + "";
  if(checkIsDouble(str) == true)
  {
    if(parseFloat(str)<=parseFloat(val))
      return true;
    else
      return false;
  }
  else
    return false;
}//~~~
function isNotNegativeDouble(str)
{
  //如果为空,则通过校验
  if(str == "")
    return true;
  if(checkIsDouble(str) == true)
  {
    if(parseFloat(str) < 0)
      return false;
    else
      return true;
  }
  else
    return false;
}//~~~
function checkIsValidDate(str)
{
  //如果为空,则通过校验
  if(str == "")
    return true;
  var pattern = /^((\d{4})|(\d{2}))-(\d{1,2})-(\d{1,2})$/g;
  if(!pattern.test(str))
    return false;
  var arrDate = str.split("-");
  if(parseInt(arrDate[0],10) < 100)
    arrDate[0] = 2000 + parseInt(arrDate[0],10) + "";
  var date = new Date(arrDate[0],(parseInt(arrDate[1],10) -1)+"",arrDate[2]);
  if(date.getYear() == arrDate[0]
    && date.getMonth() == (parseInt(arrDate[1],10) -1)+""
    && date.getDate() == arrDate[2])
    return true;
  else
    return false;
}//~~~
function checkDateEarlier(strStart,strEnd)
{
  if(checkIsValidDate(strStart) == false || checkIsValidDate(strEnd) == false)
    return false;
  //如果有一个输入为空,则通过检验
  if (( strStart == "" ) || ( strEnd == "" ))
    return true;
  var arr1 = strStart.split("-");
  var arr2 = strEnd.split("-");
  var date1 = new Date(arr1[0],parseInt(arr1[1].replace(/^0/,""),10) - 1,arr1[2]);
  var date2 = new Date(arr2[0],parseInt(arr2[1].replace(/^0/,""),10) - 1,arr2[2]);
  if(arr1[1].length == 1)
    arr1[1] = "0" + arr1[1];
  if(arr1[2].length == 1)
    arr1[2] = "0" + arr1[2];
  if(arr2[1].length == 1)
    arr2[1] = "0" + arr2[1];
  if(arr2[2].length == 1)
    arr2[2]="0" + arr2[2];
  var d1 = arr1[0] + arr1[1] + arr1[2];
  var d2 = arr2[0] + arr2[1] + arr2[2];
  if(parseInt(d1,10) > parseInt(d2,10))
    return false;
  else
    return true;
}//~~~
function checkEmail(str)
{
  //如果为空,则通过校验
  if(str == "")
    return true;
  if (str.charAt(0) == "." || str.charAt(0) == "@" || str.indexOf('@', 0) == -1
    || str.indexOf('.', 0) == -1 || str.lastIndexOf("@") == str.length-1 || str.lastIndexOf(".") == str.length-1)
    return false;
  else
    return true;
}//~~~
function checkIsChinese(str)
{
  //如果值为空,通过校验
  if (str == "")
    return true;
  var pattern = /^([一-龥]|[︰-??])*$/gi;
  if (pattern.test(str))
    return true;
  else
    return false;
}//~~~
String.prototype.realLength = function()
{
 return this.replace(/[^\x00-\xff]/g,"**").length;
}
function checkMask(str,pat)
{
  //如果值为空,通过校验
  if (str == "")
    return true;
  var pattern = new RegExp(pat,"gi")
  if (pattern.test(str))
    return true;
  else
    return false;
}//~~~
function getFilePostfix(oFile)
{
  if(oFile == null)
    return null;
  var pattern = /(.*)\.(.*)$/gi;
  if(typeof(oFile) == "object")
  {
    if(oFile.value == null || oFile.value == "")
      return null;
    var arr = pattern.exec(oFile.value);
    return RegExp.$2;
  }
  else if(typeof(oFile) == "string")
  {
    var arr = pattern.exec(oFile);
    return RegExp.$2;
  }
  else
    return null;
}

希望本文所述对大家Javascript程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/99613.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号