Click here to Skip to main content
15,921,156 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi to all!

i have an application which consist more then fifteen forms and many textboxes

i want to create a function where all checks are ther for the textbox

i have two kinds of textbox

one for int
other for char (50)
Posted
Comments
koolprasad2003 21-Sep-11 0:45am    
Means u need to validate that Texboxes. some with onlyNumbers and some with char right ?
kami124 21-Sep-11 0:52am    
yes absolutaly right
all the characters except alphabets, numeric, single _, one space between two strings e.g Allan Donald, end of the string nothing like space
and text box should not be empty
Prerak Patel 21-Sep-11 0:46am    
What do you want to check?!
kami124 21-Sep-11 0:50am    
all the characters except alphabets, numeric, single _, one space between two strings e.g Allan Donald, end of the string nothing like space
kami124 21-Sep-11 0:51am    
and text box should not be empty

Use Regular Expressions for validating textboxes.
You will find many examples of validating textboxes using Regular Expressions here[^]
 
Share this answer
 
Comments
kami124 21-Sep-11 3:21am    
ya i use it its ok but here is little problem
i make it like this
Regex regex = new Regex(@"^[a-zA-Z'' ']{1,40}$");

but i want to set only single space between the strings and no space at the end
how to do it
//following code will accept only numeric with Spaces
C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
       {

           e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar)&& !(e.KeyChar == ' ') ;

       }


//following code will accept only char's with sapces
C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{

    e.Handled = !char.IsLetter(e.KeyChar) && !char.IsControl(e.KeyChar)&& !(e.KeyChar == ' ') ;

}
 
Share this answer
 
v2
Comments
kami124 21-Sep-11 1:34am    
ok i use it like this
but its showing error
so i use KeyChar
if ((e.KeyChar < KeyChar.A) || (e.KeyChar > e.KeyChar.Z) || ((e.KeyChar < KeyChar.a) || (e.KeyChar > KeyChar.z))
e.Handled = true;

now the error is you are missing directive on KeyChar.a, KeyChar.z.....
koolprasad2003 21-Sep-11 1:50am    
try following
////////////////////
if ((e.KeyChar < 64 || e.KeyChar > 91) && (e.KeyChar < 96 || e.KeyChar > 123))
e.Handled = true;
else
e.Handled = false;
kami124 21-Sep-11 2:06am    
there is no effect on text whats so ever i type i get
like i type "asd 34 =-'/[]"
and my output is also "asd 34 =-'/[]"
so what to do
koolprasad2003 21-Sep-11 3:24am    
check the new updated code, you need to call function according to your requirement
kami124 21-Sep-11 4:04am    
i done it like this
thanks for your co operation
Regex regex = new Regex(@"^[a-zA-Z'' ']{1,40}$");

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