Click here to Skip to main content
12,356,154 members (69,720 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: regular-expression
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 25-Jun-12 1:11am
Rate this: bad
 
good
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:
///    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']
  Permalink  
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
Rate this: bad
 
good
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.
  Permalink  
v6

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


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 28 Jun 2012
Copyright © CodeProject, 1999-2016
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