天天看點

js 自動大小寫轉換

http://blog.csdn.net/cyc123007512/article/details/7337048

隻能輸入數字 

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);"

方法1:JS 方法

 <input name="test" type="text" onkeyup="this.value=this.value.toUpperCase()" /> 此種方法不好在于,光标始終在最後一個字段沒辦法移動

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

看看用CSS方法最完美的方法.

<input name="test" type="text" style="text-transform:uppercase;" />

繼續閱讀