11,642,216 members (63,000 online)
Rate this:
See more:
Hi,
TextBox should not accept only numeric values and only special characters. It can accept alpha-numeric values and alphabets.

Thanks & Regards,
Yogesh Sharma
Posted 25-Nov-12 19:40pm

Rate this:

## Solution 1

```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
```if(!char.IsLetterOrDigit(textbox1.text)
{
e.handled= true;
}```
v2
Rate this:

## Solution 2

This code is usefull to you.place the code in once cs file and apply the functions to textbox.
``` 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);

}```
v2
Rate this:

## Solution 3

```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)
v2

 0 DamithSL 240 1 Sergey Alexandrovich Kryukov 184 2 Afzaal Ahmad Zeeshan 139 3 OriginalGriff 135 4 Mika Wendelius 80
 0 Mika Wendelius 460 1 DamithSL 443 2 OriginalGriff 340 3 Afzaal Ahmad Zeeshan 228 4 Sergey Alexandrovich Kryukov 179

Web01 | 2.8.150731.1 | Last Updated 26 Nov 2012