Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how do i write RegularExpressionValidator ValidationExpression that checks that the first character must be Uppercase in a textbox ?
Posted

Actually, you just convert the first char if it's lower case using regex match and replace.

I don't see a reason to beg the user to capitalize the first char so you can validate it.

http://stackoverflow.com/questions/1668117/regex-how-to-uppercase-the-first-character-of-each-word[^]

The regex should work for validation as well, if not see below

http://stackoverflow.com/questions/8274562/regex-to-check-if-the-first-character-is-uppercase[^]
 
Share this answer
 
v2
Comments
Kurac1 2-May-13 17:58pm    
yeah i convert it yes , but even if i convert it when saving to database the first character are still lower case but in the view on the page its uppercase ?
jkirkerx 2-May-13 18:04pm    
Your not saving from the buffer you made for conversion,

In other words, you make a buffer like a string or a class or structure, you make the swap, and send the buffer, class or structure to the program that saves the data.

In your program that saves the data, don't get your values from the objects such as textbox, get your values from your string, class or structure that has formatted data.

In other words, gather your raw data first from the textboxes, then process it as refined data, then save the refined data to the database.
You might be interested in C# Regex: Supported Unicode General Categories[^].
There is a character class for upper case: \p{Lu] that matches any Unicode upper case (not only ASCII). The opposite of \p{Lu} is \P{Lu} (see upper case P), which means in the regex: all but the given class, i.e. all but upper case
E.g. error if the first character is not upper case:
C#
if (Regex.Match(input, @"^\P{Lu]").Success) { /*error...*/ }

Cheers
Andi
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-May-13 19:12pm    
5ed,
—SA
Andreas Gieriet 3-May-13 5:17am    
Thanks for your 5!
Cheers
Andi
Kurac1 3-May-13 3:35am    
i have tryed like this on button click but dont get any error and string input = TextBoxLanguages.Text;
if (Regex.Match(input, @"^\P{Lu]").Success)
{
LabelErrorLang.Visible = true;
LabelErrorLang.Text = "Börja med stor bokstav!";
}
Andreas Gieriet 3-May-13 5:17am    
What happens if you enter AAA and what if you enter aaa?
Is the method triggered at all, i.e. are you sure that this code is compiled and executed?
Cheers
Andi
Andreas Gieriet 3-May-13 5:36am    
To your original question: "how to write a ReguarExpressionValidator: see Validation in ASP.NET. This contains all the needed info to implement it in ASP.NET. Put the ValidationExpression="^\p{Lu}.*$" in the respective check. Note that the check now tells the positive pattern, i.e. begins with upper case is valid pattern. Please also note that in the expression, the whole string must be matched, namely, there is always an implicit leading ^ and trailing $ set by the system, therefore, you must give .* after the first character class in this pattern.
Cheers
Andi

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