Click here to Skip to main content
12,951,971 members (64,345 online)
Rate this:
Please Sign up or sign in to vote.
See more:

I have an asp RegularExpressionValidator that needs to validate user input for Last name, middle name and first name fields (All in separate textboxes).
Name should accept characters like £àÀâÂäÄáÁéÉèÈêÊëËìÌîÎïÏòÒôÔöÖùÙûÛüÜçÇ’ñ, for most of the data are in french and dutch. It should also include 0-9 and a-z in upper and lower case.
It only should not accept characters like: !@#$%&*()+=?/\{}[]:;<>

I tried this syntax: ^[a-zA-Z0-9£àÀâÂäÄáÁéÉèÈêÊëËìÌîÎïÏòÒôÔöÖùÙûÛüÜçÇ’'-ñ]*$
but, it still allows some of my unwanted characters like: ? !

I also tried this one: [^!@#$%&*()+=?/\{}[]:;<>]
In this one, I successfully prohibits my unwanted characters BUT it also prohibits my french characters like: £àÀâÂäÄáÁéÉèÈêÊëËìÌîÎïÏòÒôÔöÖùÙûÛüÜçÇ’'-ñ

Posted 25-Jun-12 1:11am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I wouldn't use a regex directly - I would de-regionalize my data first. This is the way I do it:
/// <summary>
/// Remove Diacritics from a string
/// This converts accented characters to nonaccented, which means it is
/// easier to search for matching data with or without such accents.
/// This code from Micheal Kaplans Blog:
/// Respaced and converted to an Extension Method
/// <example>
///    aàáâãäåçc
/// is converted to
///    aaaaaaacc
/// </example>
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static String RemoveDiacritics(this String s)
    String normalizedString = s.Normalize(NormalizationForm.FormD);
    StringBuilder stringBuilder = new StringBuilder();
    for (int i = 0; i < normalizedString.Length; i++)
        Char c = normalizedString[i];
        if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
    return stringBuilder.ToString().Normalize(NormalizationForm.FormC);

You can now use a "normal" regex to check your data only contains valid name characters [0-9a-zA-z']
jhathine 25-Jun-12 20:27pm
hmm.. My asp validator validates fields from the client side. So, if I applied this approach, it will then submit it in the server before it validates in the client side? Correct me if I'm wrong. Because the idea of asp validator is to validate fields before it submits data to the server.
jhathine 25-Jun-12 20:32pm
Here is the example of asp validator that i'm using:

<asp:RegularExpressionValidator ID="lastNameExpValidator" runat="server" ControlToValidate="LastNameTextBox" ValidationExpression="^[a-zA-Z0-9£àÀâÂäÄáÁéÉèÈêÊëËìÌîÎïÏòÒôÔöÖùÙûÛüÜçÇ’'-ñ]*$" Text="Invalid characters">

This is coded in .aspx
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Ok, now I got it. Here's the correct syntax:

This will eliminate my unwanted characters but successfully allowed french characters.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,239
CHill60 3,490
Maciej Los 3,083
Jochen Arndt 1,975
ppolymorphe 1,880

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 28 Jun 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100