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.

10 thoughts on “How to do French alphanumeric regular expression validation in ASP.NET”

  1. 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…

  2. 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

Leave a Reply

Your email address will not be published. Required fields are marked *

captcha

Please enter the CAPTCHA text