Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
TextBox should not accept only numeric values and only special characters. It can accept alpha-numeric values and alphabets.
Please give me proper ValidationExpression.

Thanks & Regards,
Yogesh Sharma
Posted

C#
StringBuilder newval = new StringBuilder();

foreach (char i in textbox1.Text)
{
    if (char.IsLetterOrDigit(i))
    {
        newval.Append(i);
    }
}
textbox1.Text = newval.GetString();

This should be done on textchanged event of textbox.

and on textbox1_keyPress event
C#
if(!char.IsLetterOrDigit(textbox1.text)
{
    e.handled= true;
}
 
Share this answer
 
v2
This code is usefull to you.place the code in once cs file and apply the functions to textbox.
C#
public static bool IsNaturalNumber(string strNumber)
       {
           Regex objNotNaturalPattern = new Regex("[^0-9]");
           Regex objNaturalPattern = new Regex("0*[1-9][0-9]*");
           return !objNotNaturalPattern.IsMatch(strNumber) &&
           objNaturalPattern.IsMatch(strNumber);
       }

       // Function to test for Positive Integers with zero inclusive
       public static bool IsWholeNumber(String strNumber)
       {
           Regex objNotWholePattern = new Regex("[^0-9]");
           return !objNotWholePattern.IsMatch(strNumber);
       }
       // Function to Test for Integers both Positive & Negative
       public static  bool IsInteger(String strNumber)
       {
           Regex objNotIntPattern = new Regex("[^0-9-]");
           Regex objIntPattern = new Regex("^-[0-9]+$|^[0-9]+$");
           return !objNotIntPattern.IsMatch(strNumber) && objIntPattern.IsMatch(strNumber);
       }
       // Function to Test for Positive Number both Integer & Real
       public static bool IsPositiveNumber(String strNumber)
       {
           Regex objNotPositivePattern = new Regex("[^0-9.]");
           Regex objPositivePattern = new Regex("^[.][0-9]+$|[0-9]*[.]*[0-9]+$");
           Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
           return !objNotPositivePattern.IsMatch(strNumber) &&
           objPositivePattern.IsMatch(strNumber) &&
           !objTwoDotPattern.IsMatch(strNumber);
       }
       // Function to test whether the string is valid number or not
       public static bool IsNumber(String strNumber)
       {
           Regex objNotNumberPattern = new Regex("[^0-9.-]");
           Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
           Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
           String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
           String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
           Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");
           return !objNotNumberPattern.IsMatch(strNumber) &&
           !objTwoDotPattern.IsMatch(strNumber) &&
           !objTwoMinusPattern.IsMatch(strNumber) &&
           objNumberPattern.IsMatch(strNumber);
       }
       // Function To test for Alphabets.
       public static bool IsAlpha(String strToCheck)
       {
           Regex objAlphaPattern = new Regex("[^a-zA-Z]");
           return !objAlphaPattern.IsMatch(strToCheck);
       }
       // Function to Check for AlphaNumeric.
       public static bool IsAlphaNumeric(String strToCheck)
       {
           Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9]");
           return !objAlphaNumericPattern.IsMatch(strToCheck);
       }
       public static bool IsAlphaNumSymbol(string symToCheck)
       {
           Regex objSpecialSymbolPattern = new Regex("[^&a-zA-Z0-9]");
           return !objSpecialSymbolPattern.IsMatch(symToCheck);

       }
 
Share this answer
 
v2
const validchars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

If Instr(validchars, KeyPressed) > 0 Then
   'its valid
else
   'its not valid
end if


Instr is the InString command and looks for instances of the second parameter in the first paramater and returns the position in the string so if its greater than 0 then it exists in the string

Command:
Instr(The comparison string, the string to search for)
 
Share this answer
 
v2

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