站长论坛

标题: 提供几个有用的Javascript验证脚本 [打印本页]

作者: tznktg    时间: 2007-10-6 10:59
标题: 提供几个有用的Javascript验证脚本
这里给大家提供几个有用的Javascript验证脚本,以下是代码片段,供大家参考。
  1. function isNum(num,low,hi) {
  2.   if(isNaN(num)||numhi)return false;
  3.   return true;
  4. }

  5. function isValidIP(v) {
  6.   nums=v.split(".");
  7.   if(nums.length!=4)return false;
  8.   for(j=0;j<4;j++){
  9.     if(!isNum(nums[j],0,255))return false;
  10.   }
  11.   return true;
  12. }

  13. function checkIP(Object,Desc,MaybeEmpty)
  14. {
  15.   var strValue= Object.value;
  16.   
  17.   if(MaybeEmpty){
  18.      if(strValue.length ==0){
  19.         return true;
  20.       }
  21.   }
  22.   
  23.   if(isValidIP(strValue)==false)
  24.   {
  25.     alert(Desc + " Format Error!");
  26.     Object.focus();
  27.     return false;
  28.   }
  29.   return true;
  30. }

  31. function checkPort(Object,Desc,lowest,MaybeEmpty)
  32. {

  33.   var pattern=/^\d{4,5}$/;
  34.   var strValue= Object.value;
  35.   
  36.   
  37.   if(MaybeEmpty){
  38.     if(strValue.length ==0){
  39.         return true;
  40.       }
  41.   }
  42.   
  43.   if(strValue.match(pattern) == null)
  44.   {
  45.     alert(Desc + " Format Error!");
  46.     Object.focus();
  47.     return false;
  48.   }
  49.   
  50.   if (strValue65535)
  51.   {
  52.       alert(Desc + " Format Error!");
  53.       Object.focus();
  54.       return false;
  55.   }
  56.   
  57.   return true;
  58. }


  59. function checkFitLongStr(Object,Desc,MaybeEmpty)
  60. {

  61.   var pattern=/^\S{0,25}$/;
  62.   var strValue= Object.value;
  63.   
  64.   if(MaybeEmpty){
  65.      if(strValue.length ==0){
  66.         return true;
  67.       }
  68.   }
  69.   if(strValue.match(pattern) == null)
  70.   {
  71.     alert(Desc + " Format Error!");
  72.     Object.focus();
  73.     return false;
  74.   }
  75.   
  76.   return true;
  77. }

  78. function checkFitLongNum(Object,Desc,MaybeEmpty)
  79. {

  80.   var pattern=/^\d{0,8}$/;
  81.   var strValue= Object.value;
  82.   
  83.   if(MaybeEmpty){
  84.      if(strValue.length ==0){
  85.         return true;
  86.       }
  87.   }
  88.   if(strValue.match(pattern) == null)
  89.   {
  90.     alert(Desc + " Format Error!");
  91.     Object.focus();
  92.     return false;
  93.   }
  94.   
  95.   return true;
  96. }

  97. function checkGC(Object,Desc)
  98. {
  99.   var pattern = /^\-{0,1}\d{1,2}$/
  100.   var strValue = Object.value;
  101.   if (strValue.length ==0){
  102.      return true;
  103.   }
  104.   
  105.   if(strValue.match(pattern) == null)
  106.   {
  107.     alert(Desc + " Format Error!");
  108.     Object.focus();
  109.     return false;
  110.   }
  111.   
  112.   if (strValue < -12 ||strValue > 18)
  113.   {
  114.     alert(Desc + " Format Error!");
  115.     Object.focus();
  116.     return false;
  117.   }
  118.   return true;
  119. }
复制代码

[ 本帖最后由 tznktg 于 2007-10-6 11:00 编辑 ]




欢迎光临 站长论坛 (https://www.tzlink.com/bbs/) Powered by Discuz! X3.2