asfman
android developer
posts - 90,  comments - 213,  trackbacks - 0

 

Posted on 2005-03-01 09:43 supersnake 阅读(446) 评论(0)  编辑 收藏收藏至365Key 所属分类: JavaScript

在做网页的过程中,总是会有不同的FORM的,提交前总是要CHECK一下FORM里面的数据的,为了方便,写了下一个程序,以使得CHECK各式表单变得那么容易.

## _form.js
///$: supersnake @ 2005-1-12

[功能] 提交前检查输入数据是否合法 
checktype: 检查类型 可以为以下的值 
empty >> 允许为空 
nempty >> 不允许空 
integer >> 整数(不可以0*) 
float >> 小数 
stander >> 只允许 0-9  a-z  A-Z  _ 
email >> email 
valid >> 不包含 #^@%()[]+-*/ 等无效字符 
number >> 只允许数字串(可以0*) 
length(maxlength,minlength) >> 长度限制 
dom(maxdom,mindom) >> 范围限制 
mustselect >> 选择框必须选择(selectIndex>0) 
checkmsg: 不合法时的提示信息 


 [例子] 
  <script src="_form.js"></script> 
  <form id="idform"> 
   <table> 
    <tr><td><input checktype="empty,float" checkmsg="要么不输入,要么输入浮点"></td> 
    <td>要么不输入,要么输入浮点</td></tr> 
    <tr><td><input checktype="empty,integer,dom" maxdom=200 mindom=0 checkmsg="要么不输入,要么输入0-200的整数"> 
    <td>要么不输入,要么输入0-200的整数</td></tr> 
   </table> 
  <input type="submit" value=" 测试 " onclick="return checkform('idform')"> 
  </form>


以下上文件 _form.js

if ( ! String.prototype.contain){ 
String.prototype.contain 
=   function (s){ 
return ( this .indexOf(s) !=- 1 ); 
}} 

function  checkform(_obj) 


if ( typeof (_obj) == " string " ){ 
var  _form  =  document.all(_obj); 
if ( ! _form) return
_obj 
=  _form; 


if (_obj.checktype){ 
var  t  =  _obj.checktype; 
var  x  =   false
var  msg = ""

if (t.contain( " nempty " &&   ! x){ 
 
if (_obj.value.length == 0 )x = true
 msg 
=   " 不能为空 "


if (t.contain( " integer " &&   ! x){ 
 reg 
=   /^ [ 1 - 9 ] + \d * $ /
 
if ( ! reg.test(_obj.value))x = true
 msg 
=   " 必须是整数 "

if (t.contain( " float " &&   ! x){ 
 reg 
=   /^ (\d + )(\.{ 0 , 1 })(\d * )$ /
 
if ( ! reg.test(_obj.value))x = true
 msg 
=   " 必须是小数 "

if (t.contain( " email " &&   ! x){ 
 reg 
=   /^ (\w + )@(\w + )(\.)(\w + )$ / i; 
 
if ( ! reg.test(_obj.value))x = true
 msg 
=   " EMAIL 格式错误 "


if (t.contain( " valid " &&   ! x){ 
 reg 
=   / [ = \ | @#\$ %- \ * \(\)\[\]\.\ " \']|(^\s*$)/; 
 if(reg.test(_obj.value))x=true; 
 msg = 
" 无效 "

if(t.contain(
" length " ) && !x){ 
 if(_obj.maxlength && _obj.value.length>_obj.maxlength){msg=
" 过长 " ;x=true;} 
 if(_obj.minlength && _obj.value.length<_obj.minlength){msg=
" 过短 " ;x=true;} 

if(t.contain(
" number " ) && !x){ 
 reg = /^\d+$/; 
 if(!reg.test(_obj.value))x=true; 
 msg = 
" 必须是数字串 "


if(t.contain(
" dom " ) && !x){ 
 if(_obj.maxdom && _obj.value*1>_obj.maxdom){msg=
" 过大 " ;x=true;} 
 if(_obj.mindom && _obj.value*1<_obj.mindom){msg=
" 过小 " ;x=true;} 


if(t.contain(
" mustselect " ) && !x){ 
 msg = 
" 请选择 "
 if(_obj.selectedIndex==0)x=true; 


if(!t.contain(
" nempty " ) && t.contain( " empty " )){ 
 if(_obj.value.length==0)x=false; 


if(x){ 
 msg = _obj.checkmsg ? _obj.checkmsg : msg; 
 if(msg!=
"" )alert(msg); 
 if(_obj.select)_obj.select() 
 else if(_obj.focus)_obj.focus(); 
 return false; 


}//~if has checktype


if(_obj.childNodes.length>0){ 

for(var i=0;i<_obj.childNodes.length;i++) 
  if(checkform(_obj.childNodes[i])==false)return false; 

}//~if

return true; 
}
posted on 2006-04-10 22:35 汪杰 阅读(214) 评论(0)  编辑 收藏 引用 所属分类: javascript
只有注册用户登录后才能发表评论。

<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(15)

随笔分类(1)

随笔档案(90)

文章分类(727)

文章档案(712)

相册

收藏夹

http://blog.csdn.net/prodigynonsense

友情链接

最新随笔

搜索

  •  

积分与排名

  • 积分 - 459575
  • 排名 - 6

最新随笔

最新评论

阅读排行榜

评论排行榜