Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void ValidateTextChar(object sender, EventArgs e)
       {
           Exception ex = new Exception();
           TextBox t = (TextBox)sender;
          char[] UnallowedData={'1','2','3','4','5','6','7','8','9','0'};
          try
          {
              if (ContainUnallowedData(t.Text,UnallowedData))
              {
                  char x = char.Parse(t.Text);
              }

          }
          catch (Exception)
          {
              try
              {
                  int CursorIndex = t.SelectionStart - 1;
                  t.Text = t.Text.Remove(CursorIndex, 1);
                  t.SelectionStart = CursorIndex - 1;
                  t.SelectionLength = 0;
              }
              catch(Exception)
              {}
          }
 public bool ContainUnallowedData(string s, char[] UnallowedData)
        {
            for (int i = 0; i < UnallowedData.Length; i++)
            
                if (s.Contains(UnallowedData[i]))
                        return true;

                     return false;
                
            
           
        }
       }

I am taking one textbox and coing coding as above and set textbox exchange is ValidateTextChar, than after running this program when i intially enetering any digit than it is not working and if i am entring alphabet intialy than it is working, please tell me why it is not working when i enter first digit
Posted
Updated 26-Jun-13 0:25am
v3
Comments
ArunRajendra 26-Jun-13 3:31am    
can you post the code of ContainUnallowedData method?
BulletVictim 26-Jun-13 3:53am    
Just want to clarify.
You are trying to limit the textbox to not allow a certain type of character input?
Does it need to always disallow these characters?
MuhammadUSman1 26-Jun-13 4:16am    
What is your requirement. as ArunRajendra said paste code for ContainUnallowedData
sakshi111 26-Jun-13 6:24am    
public bool ContainUnallowedData(string s, char[] UnallowedData)
{
for (int i = 0; i < UnallowedData.Length; i++)

if (s.Contains(UnallowedData[i]))
return true;

return false;



}
VICK 26-Jun-13 6:56am    
Have You tried This.

http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/FilteredTextBox/FilteredTextBox.aspx

What you want to insert means which character or number you want to enter in text box?

Try this with your acceptable character or number.

C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
  if (e.KeyChar == "Your acceptabl Number/character ")
  {
    e.Handled = false;
  }
  else
  {
    e.Handled = true;
  }
}
 
Share this answer
 
v2
Comments
sakshi111 26-Jun-13 6:25am    
i want to insert only charactor and symbol but not digits'
jaideepsinh 26-Jun-13 9:24am    
Use this for enter only character:
var value = "a";
var reg = new RegExp("[A-Za-z]");

if (!reg.test(value)) {
return false;
}
or you can also using RegularExpressionValidator on text box.
We can't help you because:
  • "It is not working" is really a poor assertion. You should state what is the expected behaviour and what is the observed one.
  • As already noted (see Arun Rajendra's comment) you should post the ContainUnallowedData code too.
 
Share this answer
 
I use the following to control the input into my textboxes

C#
public static bool ValidateInput(KeyPressEventArgs e)
       {
           //Get character value of keys pressed
           //If value set to true character will not be allowed in the textbox
           bool blDiscardUserInput = true;
           // A-Z
           if (e.KeyChar >= 60 && e.KeyChar <= 90)
               blDiscardUserInput = false;
           // < >
           if (e.KeyChar == 60 || e.KeyChar == 62)
               blDiscardUserInput = true;
           // a-z
           if (e.KeyChar >= 97 && e.KeyChar <= 122)
               blDiscardUserInput = false;
           // 0-9
           if (e.KeyChar >= 48 && e.KeyChar <= 57)
               blDiscardUserInput = false;
           // Backspace
           if (e.KeyChar == 8)
               blDiscardUserInput = false;
           // Comma
           if (e.KeyChar == 44)
               blDiscardUserInput = false;
           // Single Quote
           if (e.KeyChar == 39)
               blDiscardUserInput = true;
           // Space
           if (e.KeyChar == 32)
               blDiscardUserInput = false;
           // Hyphen
           if (e.KeyChar == 45)
               blDiscardUserInput = false;
           // Dot
           if (e.KeyChar == 46)
               blDiscardUserInput = false;
           // Enter
           if (e.KeyChar == 13)
               blDiscardUserInput = false;
           // !()#%_+:?`[]/*
           if (e.KeyChar == 33 || e.KeyChar == 40 || e.KeyChar == 41 || e.KeyChar == 35 || e.KeyChar == 37 || e.KeyChar == 43 || e.KeyChar == 95 || e.KeyChar == 58 || e.KeyChar == 63 || e.KeyChar == 96 || e.KeyChar == 91 || e.KeyChar == 93 || e.KeyChar == 146 || e.KeyChar == 47 || e.KeyChar == 42)
               blDiscardUserInput = false;
           return blDiscardUserInput;
       }


private void textbox_KeyPress(object sender, KeyPressEventArgs e)
       {
           //Chech if the key pressed is allowed
           e.Handled = ValidateInput(e);
       }
 
Share this answer
 
Regular expressions[^], enable you to conveniently (sort of) define either the allowed or the forbidden characters.
C#
using System.Text.RegularExpressions;

private bool IsValidInput(string input)
{
    Regex validator = new Regex("^[a-zA-Z]*$");

    Match match = validator.Match(input);
    return (match.Success);
}
This one allows empty inputs or lowercase and uppercase characters. Nothing more. No interpunction, dashes, whatsoever.
 
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