Click here to Skip to main content
15,921,694 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a textbox 'Mobile'. I'm using a C# code to check if the entered value is a number or not. But it only checks upto 9 digits. And I want 10 digits to be checked i.e. Mobile Number.

aspx

<asp:TextBox ID="Mobile" runat="server" BorderColor="Black" class="textbox" Style="text-transform:uppercase" Width="80px" MaxLength="10">


C#

int parsedValue;
if (!int.TryParse(Mobile.Text, out parsedValue))
{
Label lbl1 = new Label();
lbl1.Text = "Please Enter only Numbers in Mobile No.";
ScriptManager.RegisterStartupScript(this,this.GetType(),"ShowSuccess","javascript:Showalert('" + lbl1.Text + "')",true);
return;
}
Posted

Easy, just Change MaxLength="10" to MaxLength="11" :)
 
Share this answer
 
Use a regex instead:
C#
public static Regex isMobileNo= new Regex(@"^\d{10}$");
...
    bool IsMatch = isMobileNo.IsMatch(Mobile.Text);
 
Share this answer
 

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