简体   繁体   中英

How to validate TextBox value is string in C# Windows Form application

I'm using a TextBox to enter user's name, here I want to validate that box only having alphabets which is starting with capital and continues with simple letters. For that I use the following code, even though its validating but if I enter a number after some alphabates it is not identifying that, please some one help me to find the problem.

if (!Regex.IsMatch(textBox3.Text, @"[a-zA-Z]"))
{
    errorProvider2.SetError(textBox3, "Only use alphabates");
}

Try this:

if (!Regex.IsMatch(textBox3.Text, @"[A-Z][a-zA-Z\s\'-]*")) 
    { 
        errorProvider2.SetError(textBox3, "Only use alphabates"); 
    } 

use this pattern

^[AZ]?[az]*$

Try this:

string input = textBox3.Text;
Regex.IsMatch(input, @"^[a-zA-Z]+$");

Try this regex, It might help you.

if (!Regex.IsMatch(textBox3.Text, @"^[A-Z][A-Za-z]*$"))
{
    errorProvider2.SetError(textBox3, "Only use alphabets");
}

It starts with capital letters and be followed by zero or more letters of any case.

This will work. Satisfies all your conditions:

@"^[A-Z]{1}[a-z]+$"

[AZ]{1} - Matches the first letter to uppercase and only once.
[az]+ - Matches only lower case letters one or more times
$ - Marks the end of string, so numbers aren't matched any more

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM