i want to validate the text in a textbox as alphanumeric (a-z, A-Z, 0-9) - is there built in method for this or do I have to loop through the string and chaeck each character?
I am placing the code on the textbox changetext event.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
jitenderPosted Nov 15, 2013, 3:31 PM
Try this one:
In the example above:
VulpesPosted Apr 13, 2011, 10:31 AM
private void txtChangePasswordUsername_TextChanged(object sender, EventArgs e)
{
bool isValid = true;
foreach(char c in txtChangePasswordUsername.Text)
{
if(!char.IsLetterOrDigit(c))
{
isValid = false;
break;
}
}
if(!isValid)
{
MessageBox.Show("Enter only letters or numbers.", "Alert!");
txtChangePasswordUsername.Text = "";
txtChangePasswordUsername.Focus();
}
}
Richard BrennanPosted Apr 13, 2011, 9:17 AM
private void txtChangePasswordUsername_TextChanged(object sender, EventArgs e)
{
string text = txtChangePasswordUsername.Text;
int textLength = text.Length;
for (int i = 0; i < textLength; )
{
if(char.IsLetter(text[i]) || char.IsNumber(text[i]))
i++;
else
{
MessageBox.Show("Enter only letters or numbers.", "Alert!");
txtChangePasswordUsername.Text = "";
txtChangePasswordUsername.Focus();
i = textLength; ß could i use a break statement here?
}
}
}
Amit ChoudharyPosted Apr 13, 2011, 9:02 AM
Use below code to validation your input string in textbox
using System.Text.RegularExpressions;
// Function to Check for AlphaNumeric.
public bool IsAlphaNumeric(String strToCheck)
{
Regex objAlphaNumericPattern=new Regex("[^a-zA-Z0-9]");
return !objAlphaNumericPattern.IsMatch(strToCheck);
}
Hope this helps you.
Thanks.