linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘网络学院网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > 开发语言 > JavaScript >
栏目导栏
  php
  JSP
  ASP
  asp.net
  JAVA
  c/c++/c#
  perl
  JavaScript
  Basic
  Delphi
资料搜索
热门文章
·javascript 时间对象的格式化
·去掉字符串前后的空格
·javascript 事件监听机制
·javascript 事件调用顺序
·js刷新框架子页面的七种方法
·JavaScript:替换字符串
·IE下的JScript编程需注意的内存
·用javascript操作word文档
·Javascript中CTRL+回车提交表单
·JS 获取鼠标位置
·javascript判断Email地址是否有
·Javascript中Select的OnChange
·JS实现的滑动展开与折叠效果
·有分页功能的WEB打印
·Javascript实现窗口最大化的严
最新文章
·使用CSS改变表格边框样式
·为网页添加浮动广告
·判断表单中添加是否数字的JS与
·让浏览器状态栏动起来
·使用Javascript制作行间颜色间
·禁止用右键查看源代码
·网页侦测四法
·制作弹出公告窗口
·为网页添加特效
·网页中取消鼠标右键方法大全
·JavaScript 根据屏幕解析度显示
·如何实现浏览器上的右键菜单
·如何制作浮动广告
·让弹出窗口变得“体贴”一些
·JavaScript技巧:让网页自动穿上
Google
 
用JavaScript判断日期、数字、整数和特殊字符
[ 作者:  加入时间:2008-02-20 17:36:07  来自:Linux联盟收集整理 ]
如何在用一个程序检测所输入的内容是否合法,以下是本人自己编写的一个程序对日期、数字、整数和特殊字符进行了判断同时还可判断是正数还是负数(对数值类和整数而言) 45YLinux联盟
45YLinux联盟
< script language="javascript"> 45YLinux联盟
< !-- 45YLinux联盟
45YLinux联盟
45YLinux联盟
/** 45YLinux联盟
LogicalValue:用于判断对象的值是否符合条件,现已提供的选择有: 45YLinux联盟
integer:整型,还可判断正整型和负整型 45YLinux联盟
number :数值型,同样可判断正负 45YLinux联盟
date :日期型,可支持以自定义分隔符的日期格式,缺省是以´-´为分隔符 45YLinux联盟
string :判断一个字符串包括或不包括某些字符 45YLinux联盟
返回值: 45YLinux联盟
true或false 45YLinux联盟
45YLinux联盟
参数: 45YLinux联盟
ObjStr :对象标识符——对象名; 45YLinux联盟
ObjType:对象类型(´integer´,´number´,´date´,´string´之一) 45YLinux联盟
45YLinux联盟
其他说明: 45YLinux联盟
当对象值为空时,则返回错误。 45YLinux联盟
45YLinux联盟
Author:PPDJ 45YLinux联盟
45YLinux联盟
例子: 45YLinux联盟
example 1:要求检验输入框text1的输入数据是否是“整型”数据,若不是,则提示 45YLinux联盟
if (!LogicalValue(´text1´,´integer´)) alert(´Error: Your must input a integer number´); 45YLinux联盟
example 2:要求检验输入框text1的输入数据是否是“正整型”数据,若不是,则提示 45YLinux联盟
if (!LogicalValue(´text1´,´integer´,´+´)) alert(´Error: Your must input a positive integer number´); 45YLinux联盟
example 3:要求检验输入框text1的输入数据是否是“负整型”数据,若不是,则提示 45YLinux联盟
if (!LogicalValue(´text1´,´integer´,´-´)) alert(´Error: Your must input a negative integer number´); 45YLinux联盟
exmaple 4:要求检验输入框text1的输入数据是否是数值,若不是,则提示 45YLinux联盟
if (!LogicalValue(´text1´,´number´)) alert(´Error: Your must input a number´); 45YLinux联盟
exmaple 5:要求检验输入框text1的输入数据是否是“正”数值,若不是,则提示 45YLinux联盟
if (!LogicalValue(´text1´,´number´,´+´)) alert(´Error: Your must input a number´); 45YLinux联盟
exmaple 6:要求检验输入框text1的输入数据是否是“负”数值,若不是,则提示 45YLinux联盟
if (!LogicalValue(´text1´,´number´,´-´)) alert(´Error: Your must input a number´); 45YLinux联盟
example 7:要求检验输入框text1的输入数据是否是日期型,若不是,则提示 45YLinux联盟
if (!LogicalValue(´text1´,´date´)) alert(´Error: Your must input a logical date value´); 45YLinux联盟
若它的分隔符为A,“A”是一个变量,如常用的´-´和´/´,则用如下语法 45YLinux联盟
if (!LogicalValue(´text1´,´date´,A)) alert(´Error: Your must input a logical date value´); 45YLinux联盟
或当分隔符为´/´时 45YLinux联盟
if (!LogicalValue(´text1´,´date´,´/´)) alert(´Error: Your must input a logical date value´); 45YLinux联盟
当分隔符为´-´时,可不要参数´-´,可带上 45YLinux联盟
example 8:要求检验输入框text1的输入表示颜色的字符串是否合理,若不合理,则提示 45YLinux联盟
if (!LogicalValue(´text1´,´string´,´0123456789ABCDEFabcdef´)) alert(´Error: Your must input a logical color value´); 45YLinux联盟
example 9:要求检验输入框text1的输入表示密码的字符串是否不含“´"@#$%&^*”这些字符,若含有,则提示 45YLinux联盟
if (!LogicalValue(´text1´,´string´,´´"@#$%&^*´,false)) alert(´Error: Your password can not contain ´"@#$%&^*´); 45YLinux联盟
其中false表示不包含有某些字符,true表示必须是哪些字符,缺省值为true 45YLinux联盟
*/ 45YLinux联盟
function LogicalValue(ObjStr,ObjType) 45YLinux联盟
{ 45YLinux联盟
var str=´´; 45YLinux联盟
if ((ObjStr==null) || (ObjStr==´´) || ObjType==null) 45YLinux联盟
{ 45YLinux联盟
alert(´函数LogicalValue缺少参数´); 45YLinux联盟
return false; 45YLinux联盟
} 45YLinux联盟
var obj = document.all(ObjStr); 45YLinux联盟
if (obj.value==´´) return false; 45YLinux联盟
for (var i=2;i{ 45YLinux联盟
if (str!=´´) 45YLinux联盟
str += ´,´; 45YLinux联盟
str += ´arguments[´+i+´]´; 45YLinux联盟
} 45YLinux联盟
str=(str==´´?´obj.value´:´obj.value,´+str); 45YLinux联盟
var temp=ObjType.toLowerCase(); 45YLinux联盟
if (temp==´integer´) 45YLinux联盟
{ 45YLinux联盟
return eval(´IsInteger(´+str+´)´); 45YLinux联盟
} 45YLinux联盟
else if (temp==´number´) 45YLinux联盟
{ 45YLinux联盟
return eval(´IsNumber(´+str+´)´); 45YLinux联盟
} 45YLinux联盟
else if (temp==´string´) 45YLinux联盟
{ 45YLinux联盟
return eval(´SpecialString(´+str+´)´); 45YLinux联盟
} 45YLinux联盟
else if (temp==´date´) 45YLinux联盟
{ 45YLinux联盟
return eval(´IsDate(´+str+´)´); 45YLinux联盟
} 45YLinux联盟
else 45YLinux联盟
{ 45YLinux联盟
alert(´"´+temp+´"类型在现在版本中未提供´); 45YLinux联盟
return false; 45YLinux联盟
} 45YLinux联盟
} 45YLinux联盟
45YLinux联盟
/** 45YLinux联盟
IsInteger: 用于判断一个数字型字符串是否为整形, 45YLinux联盟
还可判断是否是正整数或负整数,返回值为true或false 45YLinux联盟
string: 需要判断的字符串 45YLinux联盟
sign: 若要判断是正负数是使用,是正用´+´,负´-´,不用则表示不作判断 45YLinux联盟
Author: PPDJ 45YLinux联盟
sample: 45YLinux联盟
var a = ´123´; 45YLinux联盟
if (IsInteger(a)) 45YLinux联盟
{ 45YLinux联盟
alert(´a is a integer´); 45YLinux联盟
} 45YLinux联盟
if (IsInteger(a,´+´)) 45YLinux联盟
{ 45YLinux联盟
alert(a is a positive integer); 45YLinux联盟
} 45YLinux联盟
if (IsInteger(a,´-´)) 45YLinux联盟
{ 45YLinux联盟
alert(´a is a negative integer´); 45YLinux联盟
} 45YLinux联盟
*/ 45YLinux联盟
45YLinux联盟
function IsInteger(string ,sign) 45YLinux联盟
{ 45YLinux联盟
var integer; 45YLinux联盟
if ((sign!=null) && (sign!=´-´) && (sign!=´+´)) 45YLinux联盟
{ 45YLinux联盟
alert(´IsInter(string,sign)的参数出错: sign为null或"-"或"+"´); 45YLinux联盟
return false; 45YLinux联盟
} 45YLinux联盟
integer = parseInt(string); 45YLinux联盟
if (isNaN(integer)) 45YLinux联盟
{ 45YLinux联盟
return false; 45YLinux联盟
} 45YLinux联盟
else if (integer.toString().length==string.length) 45YLinux联盟
{ 45YLinux联盟
if ((sign==null) || (sign==´-´ && integer<0) || (sign==´+´ && integer>0)) 45YLinux联盟
{ 45YLinux联盟
return true; 45YLinux联盟
} 45YLinux联盟
else 45YLinux联盟
return false; 45YLinux联盟
} 45YLinux联盟
else 45YLinux联盟
return false; 45YLinux联盟
} 45YLinux联盟
45YLinux联盟
/** 45YLinux联盟
IsDate: 用于判断一个字符串是否是日期格式的字符串 45YLinux联盟
45YLinux联盟
返回值: 45YLinux联盟
true或false 45YLinux联盟
45YLinux联盟
参数: 45YLinux联盟
DateString: 需要判断的字符串 45YLinux联盟
Dilimeter : 日期的分隔符,缺省值为´-´ 45YLinux联盟
45YLinux联盟
Author: PPDJ 45YLinux联盟
sample: 45YLinux联盟
var date = ´1999-1-2´; 45YLinux联盟
if (IsDate(date)) 45YLinux联盟
{ 45YLinux联盟
alert(´You see, the default separator is "-"); 45YLinux联盟
} 45YLinux联盟
date = ´1999/1/2"; 45YLinux联盟
if (IsDate(date,´/´)) 45YLinux联盟
{ 45YLinux联盟
alert(´The date´s separator is "/"); 45YLinux联盟
} 45YLinux联盟
*/ 45YLinux联盟
45YLinux联盟
function IsDate(DateString , Dilimeter) 45YLinux联盟
{ 45YLinux联盟
if (DateString==null) return false; 45YLinux联盟
if (Dilimeter==´´ || Dilimeter==null) 45YLinux联盟
Dilimeter = ´-´; 45YLinux联盟
var tempy=´´; 45YLinux联盟
var tempm=´´; 45YLinux联盟
var tempd=´´; 45YLinux联盟
var tempArray; 45YLinux联盟
if (DateString.length<8 && DateString.length>10) 45YLinux联盟
return false; 45YLinux联盟
tempArray = DateString.split(Dilimeter); 45YLinux联盟
if (tempArray.length!=3) 45YLinux联盟
return false; 45YLinux联盟
if (tempArray[0].length==4) 45YLinux联盟
{ 45YLinux联盟
tempy = tempArray[0]; 45YLinux联盟
tempd = tempArray[2]; 45YLinux联盟
} 45YLinux联盟
else 45YLinux联盟
{ 45YLinux联盟
tempy = tempArray[2]; 45YLinux联盟
tempd = tempArray[1]; 45YLinux联盟
} 45YLinux联盟
tempm = tempArray[1]; 45YLinux联盟
var tDateString = tempy + ´/´+tempm + ´/´+tempd+´ 8:0:0´;//加八小时是因为我们处于东八区 45YLinux联盟
var tempDate = new Date(tDateString); 45YLinux联盟
if (isNaN(tempDate)) 45YLinux联盟
return false; 45YLinux联盟
if (((tempDate.getUTCFullYear()).toString()==tempy) && (tempDate.getMonth()==parseInt(tempm)-1) && (tempDate.getDate()==parseInt(tempd))) 45YLinux联盟
{ 45YLinux联盟
return true; 45YLinux联盟
} 45YLinux联盟
else 45YLinux联盟
{ 45YLinux联盟
return false; 45YLinux联盟
} 45YLinux联盟
} 45YLinux联盟
45YLinux联盟
/** 45YLinux联盟
IsNumber: 用于判断一个数字型字符串是否为数值型, 45YLinux联盟
还可判断是否是正数或负数,返回值为true或false 45YLinux联盟
string: 需要判断的字符串 45YLinux联盟
sign: 若要判断是正负数是使用,是正用´+´,负´-´,不用则表示不作判断 45YLinux联盟
Author: PPDJ 45YLinux联盟
sample: 45YLinux联盟
var a = ´123´; 45YLinux联盟
if (IsNumber(a)) 45YLinux联盟
{ 45YLinux联盟
alert(´a is a number´); 45YLinux联盟
} 45YLinux联盟
if (IsNumber(a,´+´)) 45YLinux联盟
{ 45YLinux联盟
alert(a is a positive number); 45YLinux联盟
} 45YLinux联盟
if (IsNumber(a,´-´)) 45YLinux联盟
{ 45YLinux联盟
alert(´a is a negative number´); 45YLinux联盟
} 45YLinux联盟
*/ 45YLinux联盟
45YLinux联盟
function IsNumber(string,sign) 45YLinux联盟
{ 45YLinux联盟
var number; 45YLinux联盟
if (string==null) return false; 45YLinux联盟
if ((sign!=null) && (sign!=´-´) && (sign!=´+´)) 45YLinux联盟
{ 45YLinux联盟
alert(´IsNumber(string,sign)的参数出错: sign为null或"-"或"+"´); 45YLinux联盟
return false; 45YLinux联盟
} 45YLinux联盟
number = new Number(string); 45YLinux联盟
if (isNaN(number)) 45YLinux联盟
{ 45YLinux联盟
return false; 45YLinux联盟
} 45YLinux联盟
else if ((sign==null) || (sign==´-´ && number<0) || (sign==´+´ && number>0)) 45YLinux联盟
{ 45YLinux联盟
return true; 45YLinux联盟
} 45YLinux联盟
else 45YLinux联盟
return false; 45YLinux联盟
} 45YLinux联盟
45YLinux联盟
45YLinux联盟
/** 45YLinux联盟
SpecialString: 用于判断一个字符串是否含有或不含有某些字符 45YLinux联盟
45YLinux联盟
返回值: 45YLinux联盟
true或false 45YLinux联盟
45YLinux联盟
参数: 45YLinux联盟
string : 需要判断的字符串 45YLinux联盟
compare : 比较的字符串(基准字符串) 45YLinux联盟
BelongOrNot: true或false,“true”表示string的每一个字符都包含在compare中, 45YLinux联盟
“false”表示string的每一个字符都不包含在compare中 45YLinux联盟
45YLinux联盟
Author: PPDJ 45YLinux联盟
sample1: 45YLinux联盟
var str = ´123G´; 45YLinux联盟
if (SpecialString(str,´1234567890´)) 45YLinux联盟
{ 45YLinux联盟
alert(´Yes, All the letter of the string in ´1234567890´´); 45YLinux联盟
} 45YLinux联盟
else 45YLinux联盟
{ 45YLinux联盟
alert(´No, one or more letters of the string not in ´1234567890´´); 45YLinux联盟
} 45YLinux联盟
结果执行的是else部分 45YLinux联盟
sample2: 45YLinux联盟
var password = ´1234´; 45YLinux联盟
if (!SpecialString(password,´´"@#$%´,false)) 45YLinux联盟
{ 45YLinux联盟
alert(´Yes, The password is correct.´); 45YLinux联盟
} 45YLinux联盟
else 45YLinux联盟
{ 45YLinux联盟
alert(´No, The password is contain one or more letters of ´"@#$%´´); 45YLinux联盟
} 45YLinux联盟
结果执行的是else部分 45YLinux联盟
*/ 45YLinux联盟
function SpecialString(string,compare,BelongOrNot) 45YLinux联盟
{ 45YLinux联盟
if ((string==null) || (compare==null) || ((BelongOrNot!=null) && (BelongOrNot!=true) && (BelongOrNot!=false))) 45YLinux联盟
{ 45YLinux联盟
alert(´function SpecialString(string,compare,BelongOrNot)参数错误´); 45YLinux联盟
return false; 45YLinux联盟
} 45YLinux联盟
if (BelongOrNot==null || BelongOrNot==true) 45YLinux联盟
{ 45YLinux联盟
for (var i=0;i{ 45YLinux联盟
if (compare.indexOf(string.charAt(i))==-1) 45YLinux联盟
return false 45YLinux联盟
} 45YLinux联盟
return true; 45YLinux联盟
} 45YLinux联盟
else 45YLinux联盟
{ 45YLinux联盟
for (var i=0;i{ 45YLinux联盟
if (compare.indexOf(string.charAt(i))!=-1) 45YLinux联盟
return false 45YLinux联盟
} 45YLinux联盟
return true; 45YLinux联盟
} 45YLinux联盟
} 45YLinux联盟
45YLinux联盟
45YLinux联盟
//--> 45YLinux联盟
< /script> Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·JavaScript制作会反弹的浮动图片  (2008-02-20 17:14:34)
 ·Javascript技术实现真正的网上试听  (2008-02-20 16:32:51)
 ·用Javascript自动输出网页文本  (2008-02-20 15:54:41)
 ·JavaScript图片小特效5例  (2008-02-20 14:56:37)
 ·使用Javascript制作声音按钮  (2008-02-20 14:49:15)
 ·用JavaScript为你的网站加密  (2008-02-20 14:45:29)
 ·一个简单的javascript菜单  (2008-02-20 13:43:04)
 ·预装载以及JavaScript Image()对象  (2008-02-20 13:41:14)
 ·JavaScript学习:删除数组元素  (2008-02-19 16:24:44)
 ·JavaScript基础实例:如何计算24点  (2008-02-19 16:16:48)