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