?
Solved

If Textbox isnumeric delete contents

Posted on 2009-02-16
6
Medium Priority
?
889 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.
0
Comment
Question by:John Account
6 Comments
 
LVL 8

Expert Comment

by:SunnyDark
ID: 23653002
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
 
LVL 8

Expert Comment

by:SunnyDark
ID: 23653018
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
 
LVL 4

Expert Comment

by:RamanaChoudary
ID: 23653211
try this

       If IsNumeric(tbState.Text) Then
             tbState.Clear()
            tbState.Focus()
            Exit Sub
        End If
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 7

Accepted Solution

by:
keustermans earned 2000 total points
ID: 23657893
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
 

Author Comment

by:John Account
ID: 23659559
Wow, super great, keustermans. Thanks!
0
 

Author Closing Comment

by:John Account
ID: 31547457
Thank you very much!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Screencast - Getting to Know the Pipeline
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question