Click here to Skip to main content
15,921,841 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi!

i create a form which has two text box

in text box1 is for user id

where only "-" and "alphabets" are allowed in lower case

how i have to validate the text box1
Posted
Comments
walterhevedeich 26-Jul-11 4:14am    
ASP.Net? WinForms?

Handle the TextBox.KeyPress event:
private void myTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
    if (!((e.KeyChar >= 'a' && e.KeyChar <= 'z') || e.KeyChar == '-'))
        {
        e.Handled = true;
        }
    }




"i make this function can u please tell me what i have to use for "-" becouse seperator is not working"
private string RemoveNonAlphabets(string str)
            {	
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < str.Length; i++)
                {	
                    if(char.IsLetter(str[i]) || char.IsSeparator(str[i]))
                    sb.Append(str[i]);	
                }	return sb.ToString();
            }



To meet your original question objectives, use IsLower rather than IsLetter - the latter allows 'A'..'Z' as well as 'a'..'z'

Don't use IsSeparator - it checks for:
The Unicode standard recognizes three subcategories of separators:
Space separators (the UnicodeCategory.SpaceSeparator category), which includes characters such as \u0020.
Line separators (the UnicodeCategory.LineSeparator category), which includes \u2028.
Paragraph separators (the UnicodeCategory.ParagraphSeparator category), which includes \u2029.
(Taken from MSDN: http://msdn.microsoft.com/en-us/library/cta536cf.aspx[^])
Use '-' as in my example above.
 
Share this answer
 
v2
Comments
kami124 26-Jul-11 4:25am    
i make this function can u please tell me what i have to use for "-" becouse seperator is not working

private string RemoveNonAlphabets(string str)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.Length; i++)
{
if(char.IsLetter(str[i]) || char.IsSeparator(str[i]))
sb.Append(str[i]);
} return sb.ToString();
}
OriginalGriff 26-Jul-11 4:33am    
Answer updated
kami124 26-Jul-11 4:32am    
here is my function
private string RemoveNonAlphabets(string str)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.Length; i++)
{
if(char.IsLetter(str[i]) || char.IsSeparator(str[i]))
sb.Append(str[i]);
}
kami124 26-Jul-11 5:12am    
can u please tell whats the problem now
the erro is not all code path return a values

private string RemoveNonAlphabets(string str)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.Length; i++)
{
if (!((str[i] >= 'a' && str[i] <= 'z') || str[i] == '-'))
{
sb.Append(str[i]);
}
return sb.ToString();

}
}
kami124 26-Jul-11 5:16am    
sorry to disturb you
ok i got it
thanks
I assume you mean a ASP.NET page, if that is the situation then please try below,

C#
<asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="reValidatorTextBox" ControlToValidate="txtTest"
    ValidationExpression="^[a-zA-Z-]+$" runat="server" ErrorMessage="Only alphabets and -"></asp:RegularExpressionValidator>
<asp:Button ID="btnTest" runat="server" Text="Button" />


Hope it might help you :)
 
Share this answer
 
 
Share this answer
 
You can use Filtered textbox extender in ajax controls and set property as

FilterMode="ValidChars" ValidChars="qwertyuiopasdfghjklzxcvbnm-"
 
Share this answer
 
If you are using winforms, and you validate later, then try this:

foreach(char ch in TextBox1.Text.ToCharArray())
{
if(char.IsLetter(ch)== false || char.IsSeparator(ch) == false)
{
MessageBox.Show("User Id cannot contain values other than letters and separator");
}
}


Please mark as answered if you have found a correct solution.
 
Share this answer
 

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