Vinz' Blog

"Code, Beer and Music" ~ my way of being a programmer!
posts - 124, comments - 366, trackbacks - 0

My Links

News

Archives

Image Galleries

TextBox that Allows Only Numeric Inputs

This simple JavaScript  function validates the TextBox input that allows only numeric values to be entered.


<script type ="text/javascript" language="javascript" >
function ValidateText(i)
{
    if(i.value.length>0)
    {
    i.value = i.value.replace(/[^\d]+/g, '');
    }
}
</script>

In your textbox add the onkeyup event or onkeypress.

<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" onkeyup = "javascript:ValidateText(this)"></asp:TextBox>

Note: If you wan't to validate a TextBox that allows negative/positive numeric values with two decimal places then you can refer to this links below:

http://forums.asp.net/p/1323787/2645052.aspx#2645052
http://forums.asp.net/t/1356834.aspx

Print | posted on Tuesday, February 17, 2009 3:54 PM |

Feedback

No comments posted yet.
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: