[JavaScript][ASP.NET] TextBox 只能輸入數字與小數點下一位

  • 10776
  • 0

摘要:[JavaScript][ASP.NET] TextBox 只能輸入數字與小數點下一位

之前小弟做過一個讓使用者只能輸入數字與Enter鍵(2012/11/22_加入小數點)

http://www.dotblogs.com.tw/aquarius6913/archive/2012/11/17/83979.aspx

但是有網友來信說,可以正常運作但是可以輸入中文

因此小弟改了一下....如下:

 

PS:

重點就是 style="ime-mode:disabled" 這樣就會鎖定為英文模式,無法打中文

其中那個 Style 是在 IE 瀏覽器下有效用的,可以讓使用者無法在這文字方塊內使用輸入法。

 

1. 只能輸入數字


function ValidateNumber(e, pnumber) 
{
    if (!/^\d+[.]?[1-9]?$/.test(pnumber)) 
    {
        var newValue = /^\d+/.exec(e.value);
        
        if (newValue != null) 
        {  
            e.value = newValue;  
        }
        else 
        {  
            e.value = ""; 
        }
    }
    return false;
}


<asp:TextBox ID="tb_1" runat="server" style="ime-mode:disabled" onkeyup="return ValidateNumber(this,value)" ></asp:TextBox>

 

2. 只能輸入數字 + 小數點下一位


<asp:TextBox ID="tb_2" runat="server" style="ime-mode:disabled" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')"></asp:TextBox>

 

 






Y2J's Life:http://kimenyeh.blogspot.tw/