Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, What I am trying to do is have a textbox that does not permit any numbers in. I currently have the below code but it is giving me a error "operator || cannot be applied" between the "0" and the "1". I was wondering how I can fix this error and make the code do what i would like it to do please.

C#
if (TextBox1.Text == "0" ||"1"|| "2"|| "3"|| "4"|| "5" || "6"|| "7"|| "8"|| "9")
{
    MessageBox.Show("No Numbers Allowed For name");
}
Posted
Updated 19-Mar-14 20:35pm
v2
Comments
Matt T Heffron 19-Mar-14 14:47pm    
It isn't entirely clear what the restriction is:
the input cannot be any single digit number, but multiple digit numbers are OK?
(that is what your code above looks like it is attempting, as well as Solution 1)
OR
the input cannot be only digits (like Solution 3 will detect)
OR
the input cannot contain any digits?
Can you clarify the restriction?
Member 9665875 19-Mar-14 17:08pm    
the textbox must only contain letters and not numbers in any part of the textbox

You can use KeyPress event of TextBox control:

C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    char[] charsNotAllowed = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    if (charsNotAllowed.Contains(e.KeyChar))
    {
        e.Handled = true;
    }
}


Hope it helps you :)
 
Share this answer
 
A variation on Marcin's Solution 4:
C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (char.IsDigit(e.KeyChar))
    {
        e.Handled = true;
    }
}

No array required!
 
Share this answer
 
Comments
Marcin Kozub 19-Mar-14 19:09pm    
Good point :)
Try
C#
if (TextBox1.Text == "0" ||TextBox1.Text == "1"|| TextBox1.Text == "2"|| TextBox1.Text == "3"|| TextBox1.Text == "4"|| TextBox1.Text == "5" || TextBox1.Text == "6"|| TextBox1.Text == "7"|| TextBox1.Text == TextBox1.Text == "8"|| TextBox1.Text == "9")
{
    MessageBox.Show("No Numbers Allowed For name");
}


You could use RegEx instead.
C#
if ( !Regex.Match ( TextBox1.Text, "^[0-9]+$" ).Success ) {
      // TODO.
}
 
Share this answer
 
Comments
Member 9665875 19-Mar-14 16:58pm    
my only problem with the top solution which i would like to use, is it checks the first character in the textbox and not the entire textbox how can i change this so the if loop condition applies for the whole textbox
u can use key press event (vb) :
VB
e.Handled = Not Char.IsDigit(e.KeyChar)
 
Share this answer
 
Use FilteredTextBox Extender
Try this:
C#
<asp:textbox id="TextBox1" runat="server"></asp:textbox>
<cc1:filteredtextboxextender id="FilteredTextBoxExtender1"  runat="server" filtertype="Alphabets" TargetControlID="TextBox1"/>
</cc1:filteredtextboxextender>
 
Share this answer
 
v2
Use Simple Javascript Code :
C#
<script type="text/javascript" language="javascript">

function AcceptAlphabhetsOnly(event, allowPeriod) {
            var keyCode = event.which ? event.which : event.keyCode;
            if (keyCode < 48 || keyCode > 57) {
                return true;
            }
            if ((keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122) || (keyCode == 32) || ((allowPeriod == true) && (keyCode == 46))) {
                return true;
            }
            return false;
        };

    </script>




On Textbox :

C#
<asp:textbox id="txtFName" runat="server" onkeypress="return AcceptAlphabhetsOnly(event, false);" />
 
Share this answer
 
v3
Comments
Marcin Kozub 19-Mar-14 17:05pm    
Question concerns C# not JS...
Rather I'd have used int.TryParse[^].

C#
public void checkText(string value)
{
    int number;
    bool result;
    
    result = int.TryParse(value, out number);
    
    if (result)  
    {
        MessageBox.Show("No Numbers Allowed For name");
    }
}


-KR
 
Share this answer
 
Comments
Marcin Kozub 19-Mar-14 16:53pm    
Your code will not work if user enter somtehing like this: Krunal0...
Krunal Rohit 19-Mar-14 16:55pm    
That is a different thing.
My point is, instead of using multiple if, we can get some advantage of framework as well..

And have you tried this code ??

-KR
Marcin Kozub 19-Mar-14 17:03pm    
I don't have to try your code, because it's obvious that it will not work when user enter text like 'Krunal0'.

Question is very clear, OP doesn't want to user enter any digit on text. Your code displays message only when entered text is a number.

Try to read question more carefuly.

I've posted Solution #4. You should take a look.

Member 9665875 19-Mar-14 17:19pm    
this is what i would like though
Krunal Rohit 20-Mar-14 2:58am    
"Accept Solution", if this would have helped.

-KR

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