If Textbox isnumeric delete contents

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.
John AccountAsked:
Who is Participating?
 
keustermansCommented:
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

0
 
SunnyDarkCommented:
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

0
 
SunnyDarkCommented:
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
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
RamanaChoudaryCommented:
try this

       If IsNumeric(tbState.Text) Then
             tbState.Clear()
            tbState.Focus()
            Exit Sub
        End If
0
 
John AccountAuthor Commented:
Wow, super great, keustermans. Thanks!
0
 
John AccountAuthor Commented:
Thank you very much!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.