asp.net中前台限制TextBox文本框只能输入数字和小数(或数字和字母)

时间:2022-11-05 17:00:21
1.限制TextBox文本框里只能输入数字和小数
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
    function clearNoNum(event,obj)
    {
        event=window.event || event;
        if(event.keyCode==37 | event.keyCode==39)
        {return;}
        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("$#$",".");
    }
    function checkNum(obj)
    {obj.value=value.replace(/\.$/g,"");}
</script>

<asp:TextBox ID="TextBox1" onKeyUp="clearNoNum(event,this)" onBlur="checkNum(this)"></asp:TextBox>


2.限制TextBox文本框里只能输入数字和字母,不允许汉字

<asp:TextBox ID="TextBox2" runat="server" 
            onkeyup="value=value.replace(/[^\w\.\/]/ig,'')" MaxLength="6"></asp:TextBox>