validating a textbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • meena83
    New Member
    • Sep 2007
    • 5

    validating a textbox

    hi, i have to enter a value in a textbox that
    should accept only alphabets and not digits
    should accept only 4 characters
    the first should be caps and the other must be small
    --how can i do this .
  • gurpreetintown
    New Member
    • Sep 2007
    • 9

    #2
    To restrict the textbox to these input conditions, impose these validations under the value change event of the textbox.

    Comment

    • Nagendra Singh
      New Member
      • Sep 2007
      • 4

      #3
      U can use max length property of textbox to resrict number of characters.
      use keypress event of text box and manipuate the capital letter.

      if the e.KeyAscii > Keys.0 or e.KeyAscii <= Keys.0
      don't allow
      else
      allow and change the capitals by adding + 26 to ascii of small letter

      Comment

      • meena83
        New Member
        • Sep 2007
        • 5

        #4
        Originally posted by gurpreetintown
        To restrict the textbox to these input conditions, impose these validations under the value change event of the textbox.


        hi
        but still i cant understand and i didnt find that valuechange event.

        Comment

        • meena83
          New Member
          • Sep 2007
          • 5

          #5
          Originally posted by Nagendra Singh
          U can use max length property of textbox to resrict number of characters.
          use keypress event of text box and manipuate the capital letter.

          if the e.KeyAscii > Keys.0 or e.KeyAscii <= Keys.0
          don't allow
          else
          allow and change the capitals by adding + 26 to ascii of small letter

          hi
          thanks for your reply. but still i m not clear.can you send me that coding part

          Comment

          • Nagendra Singh
            New Member
            • Sep 2007
            • 4

            #6
            Code:
               private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
                    {
                        if (e.KeyChar >= '0' && e.KeyChar <= '9')
                        {
                            e.KeyChar = (char)Keys.None;
                        }
                    
                    }
            
                    private void Form1_Load(object sender, EventArgs e)
                    {
                        textBox1.MaxLength = 4;         
                  
                    }
            
                    private void textBox1_TextChanged(object sender, EventArgs e)
                    {
                        if (textBox1.Text.Length == 1)
                        {
                            char[] c = textBox1.Text.ToCharArray();
                            char[] c1 = c[0].ToString().ToUpper().ToCharArray();
                            c[0] = c1[0];
                            textBox1.Text = convertCharacterArrayToString(c);
                            textBox1.SelectAll();
                        }
               
                    }
            
                    private string convertCharacterArrayToString(char[] c)
                    {
                        string str = "";
                        foreach (char character in c)
                        {
                            str = str + character;
                        }
                        return str;
                    }

            Comment

            Working...