js限制只能输入数字,转换大小写,长度限制

来源:互联网 发布:php工具 编辑:程序博客网 时间:2024/05/10 06:45

只能输入数字 

function noNumbers(obj)  
 {  
  obj.value = obj.value.replace(/[^\d.]/g,"");
  obj.value = obj.value.replace(/^\./g,"");
  obj.value = obj.value.replace(/\.{2,}/g,".");
  obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");

}

调用:

<input type="text"   onkeyup="return noNumbers(this)" ondragenter="return false"style="ime-mode:Disabled">

 

ondragenter="return false":禁止拖内容进来

style="ime-mode:Disabled":禁止使用输入法

 

 

转大写:

function toUpperCase(id_name) {
  var nKeyCode = window.event.keyCode ;
  if(nKeyCode!=37 && nKeyCode!=39 && nKeyCode!=8)
  {
   var obj = document.getElementById(id_name)
   var pos = getPos(obj);  //获取光标位置
   upperCase(obj);  //小写转大写
   setPos(obj,pos);  //设置光标位置
  }
}

调用:

<input type="text"  name="cyc'  onkeypress="toUpperCase('cyc');">

 

长度限制:

function checklength(obj,len){
 var str=obj.value;
 if(str.length>len){
  alert("输入长度不能超过:"+len);
  obj.value=str.substring(0,4);
  obj.focus();
  return ;
 }
}

 

调用:

<input type="text"  name="cyc'  onkeypress="checklength(this,4);"

原创粉丝点击