Click here to Skip to main content
15,891,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!

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: £àÀâÂäÄáÁéÉèÈêÊëËìÌîÎïÏòÒôÔöÖùÙûÛüÜçÇ’'-ñ

Why?
Posted

I wouldn't use a regex directly - I would de-regionalize my data first. This is the way I do it:
C#
/// <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:
///    http://blogs.msdn.com/b/michkap/archive/2007/05/14/2629747.aspx
/// 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)
            {
            stringBuilder.Append(c);
            }
        }

    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']
 
Share this answer
 
Comments
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
Ok, now I got it. Here's the correct syntax:
C#
^[^#$%^*@!?,><;]*$


This will eliminate my unwanted characters but successfully allowed french characters.
 
Share this answer
 
v6

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