By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,992 Members | 934 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,992 IT Pros & Developers. It's quick & easy.

validating a textbox

P: 5
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 .
Sep 12 '07 #1
Share this Question
Share on Google+
5 Replies


P: 9
To restrict the textbox to these input conditions, impose these validations under the value change event of the textbox.
Sep 12 '07 #2

P: 4
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
Sep 12 '07 #3

P: 5
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.
Sep 12 '07 #4

P: 5
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
Sep 12 '07 #5

P: 4
Expand|Select|Wrap|Line Numbers
  1.    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  2.         {
  3.             if (e.KeyChar >= '0' && e.KeyChar <= '9')
  4.             {
  5.                 e.KeyChar = (char)Keys.None;
  6.             }
  7.  
  8.         }
  9.  
  10.         private void Form1_Load(object sender, EventArgs e)
  11.         {
  12.             textBox1.MaxLength = 4;         
  13.  
  14.         }
  15.  
  16.         private void textBox1_TextChanged(object sender, EventArgs e)
  17.         {
  18.             if (textBox1.Text.Length == 1)
  19.             {
  20.                 char[] c = textBox1.Text.ToCharArray();
  21.                 char[] c1 = c[0].ToString().ToUpper().ToCharArray();
  22.                 c[0] = c1[0];
  23.                 textBox1.Text = convertCharacterArrayToString(c);
  24.                 textBox1.SelectAll();
  25.             }
  26.  
  27.         }
  28.  
  29.         private string convertCharacterArrayToString(char[] c)
  30.         {
  31.             string str = "";
  32.             foreach (char character in c)
  33.             {
  34.                 str = str + character;
  35.             }
  36.             return str;
  37.         }
  38.  
Sep 12 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.