职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 1532|回复: 0

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

[复制链接]
胖哥 发表于 2007-8-13 12:52 | 显示全部楼层 |阅读模式
这里给大家提供几个有用的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;
}
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-5-3 06:34 , Processed in 0.127096 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表