表单特殊输入js验证

作者:cnbruce 时间:2008-03-26 12:01:00 

js表单验证只能是写限定的东西大收集 代码如下:

ENTER键可以让光标移到下一个输入框




<input onkeydown="if(event.keyCode==13)event.keyCode=9" > 

只能是中文


<input onkeyup="value=value.replace(/[ -~]/g,'')" onkeydown="if(event.keyCode==13)event.keyCode=9"> 

只能是英文和数字.屏蔽了输入法



<input style="ime-mode:disabled" onkeydown="if(event.keyCode==13)event.keyCode=9"> 

只能输入英文和数字



<input onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" onkeydown="if(event.keyCode==13)event.keyCode=9"> 

只能是数字



<input onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"> 

只能显示,不能修改的文本框


<input readOnly value="只能显示,不能修改"> 

只能是数字,判断按键 代码如下:

<script  language=javascript>
function  onlyNum()
{
if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105)||(event.keyCode==8)))
event.returnValue=false;
}
</script>
<input  onkeydown="onlyNum();">  

限制网页用键盘



<body onkeydown="alert('禁用');return false;"> 

限制键盘的某个键:




<body onkeydown="if(event.keyCode==num){alert('禁用');return false;}> 

再加个找按键的值

<script>
function show(){
  alert("ASCII代码是:"+event.keyCode);
}
</script>
<body onkeydown="show()">

只能是IP地址




<html> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<style>
.a3{width:30;border:0;text-align:center}
</style>
<script>
function mask(obj){
obj.value=obj.value.replace(/[^\d]/g,'')
key1=event.keyCode
if (key1==37 || key1==39)
{ obj.blur();
nextip=parseInt(obj.name.substr(2,1))
nextip=key1==37?nextip-1:nextip+1;
nextip=nextip>=5?1:nextip
nextip=nextip<=0?4:nextip
eval("ip"+nextip+".focus()")

if(obj.value.length>=3) 
if(parseInt(obj.value)>=256 || parseInt(obj.value)<=0)
{
alert(parseInt(obj.value)+"IP地址错误!")
obj.value=""
obj.focus()
return false;
}
else 
{ obj.blur(); 
nextip=parseInt(obj.name.substr(2,1))+1
nextip=nextip>=5?1:nextip
nextip=nextip<=0?4:nextip
eval("ip"+nextip+".focus()")
}
}
function mask_c(obj)
{
clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))

</script>
<title>IP地址输入</title> 
</head>
<body>IP地址输入
<div style="border-width:1;border-color:balck;border-style:solid;width:165;font-size:9pt">
<input type=text name=ip1 maxlength=3 class=a3 onkeyup="mask(this)" onbeforepaste=mask_c()>.
<input type=text name=ip2 maxlength=3 class=a3 onkeyup="mask(this)" onbeforepaste=mask_c()>.
<input type=text name=ip3 maxlength=3 class=a3 onkeyup="mask(this)" onbeforepaste=mask_c()>.
<input type=text name=ip4 maxlength=3 class=a3 onkeyup="mask(this)" onbeforepaste=mask_c()>
</div>
</body> 
</html> 

用#default#savehistory防止后退清空text文本框:

<HTML>
<HEAD>
<META NAME="save" CONTENT="history">
<STYLE>
  .saveHistory {behavior:url(#default#savehistory);}
</STYLE>
</HEAD>
<BODY>
<INPUT class=saveHistory type=text id=oPersistInput>
<input type=button onclick='javascript:location.href="http://www.webjx.com/"' value='点击进入,再按后退键试试?'>
</BODY>
</HTML>

TEXTAREA自适应文字行数的多少


<textarea rows=1 name=s1 cols=27 onpropertychange="this.style.posHeight=this.scrollHeight"> 

上传预览图片


<img id=pic  src=/upimg/allimg/080326/0927450.gif>
<input type=file name=file><input type=button onclick=pic.src=file.value value=预览图片>
<input type=button onclick=alert(file.value) value=图片地址>
<input type=button onclick="file.outerHTML=file.outerHTML.replace(/value=\w/g,'')" value="清除file里字"> 

去掉下拉选项的边框

<div style="position: absolute; left: 10px; top: 10px; width: 115px; height: 20px;  clip:rect(2 114 20 2);"> 
   <select>
    <option >cnpeople</option>
    <option >cnrose</option>
   <option >cnbruce</option>  
  </select>
  </font>
</div> 

标签:表单,验证,js
0
投稿

猜你喜欢

  • mssql中获取指定日期所在月份的第一天的代码

    2011-09-30 11:23:57
  • 初识Firebug 全文 — firebug的使用

    2007-10-23 12:54:00
  • Oracle 存储过程总结(一、基本应用)

    2009-07-07 10:21:00
  • ASP 正则表达式常用的几种方法(execute、test、replace)

    2010-03-02 20:23:00
  • MySQL存储过程中的sql_mode问题

    2011-01-04 19:50:00
  • 通过事务日志解决SQL Server常见四大故障(二)

    2009-03-25 13:51:00
  • 正计时JS代码

    2008-05-25 14:53:00
  • ASP调用系统ping命令代码

    2008-04-27 20:45:00
  • iframe框架用JavaScript子页面控制父页面

    2009-01-19 13:43:00
  • HTML5 离线存储之Web SQL

    2011-06-19 14:13:19
  • oracle用什么SQL语句判断表存不存在

    2010-07-23 13:23:00
  • CSS 超链接图标规范 V1.0

    2007-12-28 12:05:00
  • asp的系统变量ServerVariables (“HTTP_USER_AGENT“)问题

    2009-02-04 15:51:00
  • [组图]手把手教你制作ASP留言本

    2007-09-22 09:32:00
  • RC4经典加密算法asp/VBs版本代码

    2008-02-17 17:32:00
  • iframe 的用法与注意事项

    2008-02-12 12:56:00
  • 经典分享MySQL的limit查询优化

    2011-05-05 15:47:00
  • CSS的另类拼图___减少HTTP请求

    2009-05-28 19:05:00
  • 做了CDN加速的ASP网站获取用户真实IP程序

    2011-02-16 10:59:00
  • JS重现80后儿时经典拼板(模板)游戏

    2011-09-11 18:36:46
  • asp之家 网络编程 m.aspxhome.com