Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello guys,
sorry but i m new in C#....
can any buddy tell me how to define validation for text box like numbers only,char.only etc.... actually my problem is that i want to define these function.in such way ....i can call this function directly as i need in different-2 form ....

please can any buddy give me idea what i have to do......?


C# window application
Posted

A number of Windows Forms validations techniques are discussed here[^].
Amongst them, Validation with the MaskedTextBox Control and Event Driven validation are more common approaches.

Have a look at the validating event as well[^].
Finally, here[^] is a very simple example to get you started.
 
Share this answer
 
Create a static class such as ReqFunctions. Make the methods as static and then you can call them from the form that you are working.

for example on top of my head:
C#
class ReqFunctions
{
  public static bool ValidateTextBox(string txtBoxValue, string validationType)
  {    
    if(validatioType == "Number")
    {
      Regex isnumber = new Regex("[^0-9]");
      return !isnumber.IsMatch(txtBoxValue);    
    }
    if(validationType == "Char")
    {
      Regex ischar = new Regex("^[a-zA-Z]+$");
      return !ischar.IsMatch(txtBoxValue);     
    }
  }
}



In your code you can call this function as
C#
ReqFunctions.ValidateTextBox(txtTextbox1.Text);


You can also check this MSDN link[^].
Recently I have found this also: Numbers and Characters only Textbox Validation in C# ![^]
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 14-Dec-11 3:44am    
Where do you input information on a text box? This method cannot make any sense.
--SA
Orcun Iyigun 14-Dec-11 11:30am    
Ok I have provided a better solution. Should be fine now!

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