Skip to content

How to do French alphanumeric regular expression validation in ASP.NET

If you’re working on ASP.NET, you have realized that regular expression validation can come in handy more than one time. Also, regular expressions are very easy to implement (provided you have a good knowledge about it).

Some days back, I was faced with a small task. I had to make implement Javascript regular expression validation on a field. The requirement was simple; the field should have minimum three alphanumeric characters, including French characters. For you to have an idea, French characters include “accent aigu” and “accent grave” (you can read about French accents on wikipedia).

Okay so I had the requirement and I was all set. I created a RegularExpressionValidator element on my ASP.NET page and I added “[a-zA-Z0-9 ]” as ValidationExpression. The code looked like this:

<asp:TextBox ID="txtName" runat="server" />
<asp:RegularExpressionValidator ID="rexName" ControlToValidate="txtName"
  ValidationExpression="[a-zA-Z0-9 ][a-zA-Z0-9 ][a-zA-Z0-9 ]+"
  ErrorMessage="Please enter at least 3 alphanumeric characters." runat="server" />

This worked perfectly. It matched three alphanumeric characters or more. But the problem was it was not matching any of the French characters. After some Google searching and some code trial and errors, I came up with this “[a-zA-ZÀ-ÿ0-9 ]” as ValidationExpression. This worked like a charm. The new code was like this:

<asp:TextBox ID="txtName" runat="server" />
<asp:RegularExpressionValidator ID="rexName" ControlToValidate="txtName"
  ValidationExpression="[a-zA-ZÀ-ÿ0-9 ][a-zA-ZÀ-ÿ0-9 ][a-zA-ZÀ-ÿ0-9 ]+"
  ErrorMessage="Please enter at least 3 alphanumeric characters." runat="server" />

If you can’t see the logic, let me explain. The code now matches

  • all characters from a to z (lowercase invariants)
  • all characters from A to Z (uppercase invariants)
  • all characters from À to ÿ (all accent characers including French accents in uppercase and lowercase)
  • all digits from 0 to 9 (note that the requirement did not require the name to start with a non-digit)
  • whitespaces (whitespace was not catered for in the requirement)

So there you go. You now have the regular expression validation for French characters. The beauty of regular expression validation is that it is hard to come up with, but it is very easy to implement.

Published inCoding
  • Pingback: Tweets that mention How to do French alphanumeric regular expression validation in ASP.NET | Technoreply -- Topsy.com()

  • Dude76

    Normaly, for accents, you can use \p{L}, and msdn says it’s supported by .NET; You can also check it on many websites (regexp testers).
    But, I don’t know why, although the regularexpressionvalidator accepts it as a valid expression, it refuses “french” inputs.
    I’m actually stuck with that.
    Is that an aps.Net bug ? dunno…

  • @Dude76

    This is strange. Since on my side it works perfectly. If you are referring to “\p{L}”, I have no clue. I will have to try that.

  • why not use simply the range \u00C0-\u00FF??

    • Hmmm. Never used that. Can you post an example, so we can see?

      Thanks

  • tympg

    I think “a-zA-ZÀ-ÿ” can be shortened to “A-ÿ”

    • Never tried it, but I guess it would work.

    • Very much after the fact, but in case anyone else is seeking to solve this problem, I used the “A-ÿ” solution and it worked perfectly. 🙂

  • soniya

    Hi, i need same regex pattern to validate character for “German”, spanish, Russian. Please reply ASAP.

    like french [a-zA-ZÀ-ÿ0-9 ]

    Please provide us in above pattern.

    Thanks in advance

  • Jonas

    You are the best.
    It took me so much time to find the right answer.
    You are the choosen one!!! 😛