We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

If Textbox isnumeric delete contents

Medium Priority
908 Views
Last Modified: 2013-12-17
I have a textbox that is intended to receive Customer's name. However, it can be populated with a number key when a radiobutton item list is selected. See http://www.versacore.info/Telemarketing/Begin.aspx from which I had received Expert help here http://www.versacore.info/Telemarketing/Begin.aspx

So, what I want to do now, is that, upon page load, run an if then statement to see if that textbox contains only numeric value, and if it does, delete it. So that it will remain empty for user input. I tried "If Textbox isnumeric then" but that's not possible. Please advise. Thanks.
Comment
Watch Question

You may try to use Try-Catch statement to try to convert the Textbox.Text into a number with Convert.ToInt32(...) , if there is no number in the textbox this will fail and you will do nothing (have an empty catch block) if it succeeds then clear the textbox (Textbox.Text = "")

Like this:
Try
Convert.ToInt32(myTextbox.Text)
myTextbox.Text = ""
End Try
Catch Exception
End Catch

Open in new window

I also had a look on that page you are making , and honestly those things are better done with JavaScript(meaning on the client side) or even better with AJAX extensions for .NET 3.5
try this

       If IsNumeric(tbState.Text) Then
             tbState.Clear()
            tbState.Focus()
            Exit Sub
        End If
Solutions Architect
CERTIFIED EXPERT
Commented:
Hi, I would prevent people from entering numbers all together with javascript.

In the code you will see that the entering of numbers is prevented in the first text box and entering letters is prevented in the second textbox.





<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
    <SCRIPT LANGUAGE="JavaScript">
function LettersOnly(evt) 
{
    evt = (evt) ? evt : window.event
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57)) 
    {
        return true
    }
    return false
}
function NumbersOnly(evt) 
{
    evt = (evt) ? evt : window.event
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57)) 
    {
        return false
    }
    return true
}
</SCRIPT>
</head>
<body>
    <form id="form1" runat="server">
    <div>
         <asp:TextBox ID="TextBox1" runat="server" onKeyPress="return LettersOnly(event)"></asp:TextBox><asp:Label
             ID="Label1" runat="server" Text="Label">No Numbers</asp:Label><br />
         <asp:TextBox ID="TextBox2" runat="server" onKeyPress="return NumbersOnly(event)"></asp:TextBox><asp:Label
             ID="Label2" runat="server" Text="Label">No Letters</asp:Label>
 
    </div>
    
    </form>
</body>
</html>

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Wow, super great, keustermans. Thanks!

Author

Commented:
Thank you very much!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.