Click here to Skip to main content
15,899,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to get enter only digits like in This Format "1234.56" in Textbox using validation control in asp.net.
Posted
Updated 1-Apr-11 0:01am
v2
Comments
Wild-Programmer 1-Apr-11 3:58am    
Very easy. Search google ;)

use regular expression validation and put this ^[0-9]*\.?[0-9]+$ in expression
 
Share this answer
 
The regex pattern you need to use would be "^\d+(\.\d+)?$" (without the quotes) as the previously suggested ones wouldn't account for the decimal point. You could use it in the following ways:

Using a RegularExpressionValidator Control:

<pre lang="xml"><%@ language="C#" %>
<form id="form1" runat="server">
    <asp:TextBox ID="textBox1" runat="server"/>
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
    <asp:RegularExpressionValidator ID="regexpName" runat="server"
                                    ErrorMessage="This is not a valid number."
                                    ControlToValidate="textBox1"
                                    ValidationExpression=@"^\d+(\.\d+)?$"/>
</form>


Or using the Regex class:

if(System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, @"^\d+(\.\d+)?$"))
{
    doSomething();
}
 
Share this answer
 
v3
You also might want to consider a MaskedTextBox[^] which would only allow numerics...
 
Share this answer
 
you can easily use Regular expression validator for this:
In the validation expression property keep ^\d+$.
XML
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="Please Enter Only Numbers" ValidationExpression="^\d+$" ValidationGroup="check"></asp:RegularExpressionValidator>
 
Share this answer
 
v2
Comments
Sonu Babby 1-Apr-11 4:07am    
Thanks .
You need to use Regular expression validator and push in a pattern to verify numbers, thats it. In the validation expression use "^\d+$"

Hope that helps.
 
Share this answer
 
Comments
Sonu Babby 1-Apr-11 4:07am    
Thanks .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900