提供几个有用的Javascript验证脚本

| |
[2007/01/29 20:15 | 分类: Html技术 | by gdgzboy ]
[此文来源于互联网,牛C网只负责收集整理]

这里给大家提供几个有用的Javascript验证脚本,以下是代码片段,供大家参考。

function isNum(num,low,hi) {  if(isNaN(num)||num<low||num>hi)return false;  return true;}function isValidIP(v) {  nums=v.split(".");  if(nums.length!=4)return false;  for(j=0;j<4;j  ){    if(!isNum(nums[j],0,255))return false;  }  return true;} function checkIP(Object,Desc,MaybeEmpty){  var strValue= Object.value;    if(MaybeEmpty){     if(strValue.length ==0){        return true;      }  }    if(isValidIP(strValue)==false)  {    alert(Desc   " Format Error!");    Object.focus();    return false;  }  return true;}function checkPort(Object,Desc,lowest,MaybeEmpty){  var pattern=/^\d{4,5}$/;  var strValue= Object.value;      if(MaybeEmpty){    if(strValue.length ==0){        return true;      }  }    if(strValue.match(pattern) == null)  {    alert(Desc   " Format Error!");    Object.focus();    return false;  }    if (strValue<lowest || strValue >65535)  {      alert(Desc   " Format Error!");      Object.focus();      return false;  }    return true;}function checkFitLongStr(Object,Desc,MaybeEmpty){  var pattern=/^\S{0,25}$/;  var strValue= Object.value;    if(MaybeEmpty){     if(strValue.length ==0){        return true;      }  }  if(strValue.match(pattern) == null)  {    alert(Desc   " Format Error!");    Object.focus();    return false;  }    return true;}function checkFitLongNum(Object,Desc,MaybeEmpty){  var pattern=/^\d{0,8}$/;  var strValue= Object.value;    if(MaybeEmpty){     if(strValue.length ==0){        return true;      }  }  if(strValue.match(pattern) == null)  {    alert(Desc   " Format Error!");    Object.focus();    return false;  }    return true;}function checkGC(Object,Desc){  var pattern = /^\-{0,1}\d{1,2}$/  var strValue = Object.value;  if (strValue.length ==0){     return true;  }    if(strValue.match(pattern) == null)  {    alert(Desc   " Format Error!");    Object.focus();    return false;  }    if (strValue < -12 ||strValue > 18)  {    alert(Desc   " Format Error!");    Object.focus();    return false;  }  return true;}
    


            


作者:gdgzboy@牛C网
地址:http://www.niuc.net/post/3269/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!



评论(0) | 引用(0) | 阅读(126)
发表评论
昵称 [注册]
密码 游客无需密码
网址
电邮
打开HTML 打开UBB 打开表情 隐藏 记住我