有用的正则表达式或JS方法

1、限制文本框只能输入汉字、字母和数字

js里方法:

         function rep(obj)
        { 

            //加上这一句的作用是为了方向键能用
            if(event.keyCode==37||event.keyCode==39) return;           
            obj.value=obj.value.replace(/[^\w\u4E00-\u9FA5]/g, '');
        }

调用:

前台:

onkeyup="rep(this);

后台方法:

this.txtName.Attributes.Add("onKeyup", "rep(this);");

2、限制文本框输入长度

        //字符长度
        function textLimitCheck(thisArea, maxLength)
        {
            if (thisArea.value.length > maxLength)
            {
                alert(maxLength + ' 个字限制. \r超出的将自动去除!');
                thisArea.value = thisArea.value.substring(0, maxLength);
                thisArea.focus();
            }
        }

 3、js的四舍五入的函数用法

 

<html>
<head><title>ssss</title></head>
<script>
 function toFix(idvalue){
 alert(parseFloat(idvalue).toFixed(2));//2表示保留2位小數
 
 
 }
</script>
<body>
<table><tr><td>
 <input type="text" name="aaa" onchange="toFix(this.value)">
</td></tr></table>
</body>
</html>

 

posted on 2009-06-11 13:30  心的感觉  阅读(336)  评论(0编辑  收藏  举报

导航