Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Full Name Validation - opening a can of worms ?

Last post 07-11-2006, 10:18 PM by masster. 26 replies.
Page 1 of 2 (27 items)   1 2 Next >
Sort Posts: Previous Next
  •  07-07-2006, 3:05 PM 19388

    Full Name Validation - opening a can of worms ?

    Hi,
    I'm using CodeCharge Studio 3 on PHP.
    I'm trying to build a validation regexp for a simple registration form. Main focus is on client's name (one single field).

    So here is what I am trying to achieve:

    - a valid name is formatted as: Albert Einstein 3 (last digit is because I need unicity of names and this is the best I could find)
    - invalid formats are: leading and trailing spaces, more than one space in between, non-first-letter-capitalized

    Why a can of worms? Because I feel that human imagination in entering wrong names is infinite :).

    But maybe we can sort things out.
    I am open to any solution. Thanks.
  •  07-07-2006, 3:12 PM 19389 in reply to 19388

    Re: Full Name Validation - opening a can of worms ?

    yes, a HUGE can of worms..

    u say

    Albert Einstain

    is a valid name. Then what name is invalid:? imagine

    Loretta M.J.H Mac-Hogan-O'Brien, Jr.

    in your logic this name is probably invalid, but Loretta might have a quite different opinion on this..

    ..next Q is how are u going to validate the above name?

    Maybe u just need to introduce 3 text boxes for Fname, Mname, Lname?

  •  07-07-2006, 4:08 PM 19391 in reply to 19389

    Re: Full Name Validation - opening a can of worms ?

    Further details:
    I don't need an UNIVERSAL validation regexp, but only for my country, where name of a person is made of ONLY first name + last name (2 words) and ONLY letters are used. I hope it's easier now.
  •  07-07-2006, 4:19 PM 19392 in reply to 19389

    Re: Full Name Validation - opening a can of worms ?

    ^[A-Z][a-z]*\x20[A-Z][a-z]*\x$

    will validate

    Albert Einstein

    w/o preceding and trailing white spaces, with only 1 white space in between; with both names capitalized [Use Ignore Case OFF]

    also will validate names like

    A B 

    or u want to validate only names with lenghth>1 char?

    ..and finally what country is it, anyway?  r u serious about names consisting of only [a-z] chars? ..just curious, never heard of countries like this..

  •  07-07-2006, 4:22 PM 19393 in reply to 19389

    Re: Full Name Validation - opening a can of worms ?

    ^[A-Z][a-z]*\x20[A-Z][a-z]*\x$

    will validate

    Albert Einstein

    w/o preceding and trailing white spaces, with only 1 white space in between; with both names capitalized [Use Ignore Case OFF]

    also will validate names like

    A B 

    or u want to validate only names with lenghth>1 char?

    ..and finally what country is it, anyway?  r u serious about names consisting of only [a-z] chars? ..just curious, never heard of countries like this..

  •  07-07-2006, 4:23 PM 19394 in reply to 19393

    Re: Full Name Validation - opening a can of worms ?

    sorry for a typo, should be :

    ^[A-Z][a-z]*\x20[A-Z][a-z]*$

  •  07-07-2006, 4:52 PM 19395 in reply to 19394

    Re: Full Name Validation - opening a can of worms ?

    Excellent job, Sergei.
    I'm from Romania.

    Can we get it to an upper level? Many users love to have Caps Lock on (grrr) when filling forms, so how would that regexp look like if I'd want to accept ALBERT EINSTEIN, but format it automatically as Albert Einstein? I hate having garbage data in my DB.

    Cheers
  •  07-07-2006, 4:55 PM 19396 in reply to 19393

    Re: Full Name Validation - opening a can of worms ?

    Sergei Z:

    ..and finally what country is it, anyway?  r u serious about names consisting of only [a-z] chars? ..just curious, never heard of countries like this..



    LOL

    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  07-07-2006, 5:04 PM 19397 in reply to 19395

    Re: Full Name Validation - opening a can of worms ?

    masster:
    Excellent job, Sergei. I'm from Romania. Can we get it to an upper level? Many users love to have Caps Lock on (grrr) when filling forms, so how would that regexp look like if I'd want to accept ALBERT EINSTEIN, but format it automatically as Albert Einstein? I hate having garbage data in my DB. Cheers


    The regex would reformat nothing, that would be your job to provide the code to do so.  If you were coding with a .Net language you could write a delegate to perform the reformatting.

    As for your name restrictions that's just wishful thinking.  Even if everyone born there has only a-z in there names is no one allowed to move there is their name contain any other characters? Names can't be validated only restricted, meaning you reject some valid names.

    Michael

    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  07-07-2006, 5:19 PM 19398 in reply to 19395

    Re: Full Name Validation - opening a can of worms ?

    the task like this will require some code..if u worked in Perl, it could be (possibly; Xicheng would know better) to do in one step..but u r PHP guy so u'll have ot use a delegate function for this..Regex will call a function to do an actual string processing: rendering Upper case to lower one.. 

    this piece of code (C#.NET) will give u a general idea about using delegates in Regex text processing:

    the task: render string 'abcdefgh' to 'abCDfgh', i.e match 'cde' , thne render it to 'CD' inside Regex.Replace()


    code:

    using System;
    using System.Text.RegularExpressions;

    class ToUpperCase


     static string MyReplace(Match m)
     {
      // will render a string matched by
      //delegate MatchEvaluator to upper case
      string x = m.ToString().ToUpper();
      //delete the third char before returning to Main
      x = x.Substring(0, x.Length -1);
      return x;            // x = "CD"
     }
       
     static void Main()
     { 
      //input string
      string OrigText = "abcdefgh";
       
      //declare regex pattern
      string pattern = @"(c)(d)(e)";

      //using STATIC Replace method, replace 'cde' with what is returned by function 'MyReplace': CD
      string ModifiedText = Regex.Replace(OrigText , pattern ,
       new MatchEvaluator(ToUpperCase.MyReplace), RegexOptions.IgnoreCase|);

      //print result of Replace
      Console.WriteLine("ModifiedText = " + ModifiedText);
      Console.WriteLine("\nPress Enter to Exit");
      Console.ReadLine();

      //returns: 'abCDfgh'
     
     }//end of main
    }//end of class

  •  07-07-2006, 6:37 PM 19400 in reply to 19397

    Re: Full Name Validation - opening a can of worms ?

    mash:
    As for your name restrictions that's just wishful thinking.  Even if everyone born there has only a-z in there names is no one allowed to move there is their name contain any other characters? Names can't be validated only restricted, meaning you reject some valid names.
    Your observation comes with a solution also?
  •  07-07-2006, 7:00 PM 19403 in reply to 19400

    Re: Full Name Validation - opening a can of worms ?

    the task the way u formulated it does not have a solution: u cannot validate human names 100 %; the logic u r trying to implement will inevitably reject a certain (significant) part of valid names. And this is not your fault: human names are constructed without a rigid, well-defined pattern, hence u cannot construct a regex to match/validate them.

    U really need to rethink the way u r designing your data acqusition/validation process [for names].

  •  07-09-2006, 6:01 PM 19421 in reply to 19400

    Re: Full Name Validation - opening a can of worms ?

    masster:
    mash:
    As for your name restrictions that's just wishful thinking.  Even if everyone born there has only a-z in there names is no one allowed to move there is their name contain any other characters? Names can't be validated only restricted, meaning you reject some valid names.
    Your observation comes with a solution also?


    The solution is not to try to validate a name.  It can not be done with regex, because there is truly no restriction on what a person can be named.  The best you can do with a regex is limit input to the most comman characters used in a name which are alphas, hyphens, apostrophes and periods.  But having additional characters doesn't make a name invalid.

    Michael

    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  07-09-2006, 7:13 PM 19423 in reply to 19421

    Re: Full Name Validation - opening a can of worms ?

    You are both right. So now I try a new (and final) approach, I know it is not optimum, but it will serve for my purposes.

    So I have one field named "Client Name" which includes both person names and company names. Again, plz don't jump on me :) for this idea.

    So my Q is: how do i have to modify this reg. exp.

    ^([0-9A-Z '-.]+)$

    in order to have any string of names in uppercase, but without leading/trailing spaces and 'space' character should only have one succesive repetition ?

    Valid names:
    ALBERT EINSTEIN 2000 LTD.
    ALB-ERT EINSTEIN WITH MANY SURNAMES FOLLOWING
    3M

    INvalid names:
    albert EinsTein
    (space)Albert(space)(space)Einstein(space)

    Hope it's clear. I have a headache.

    Cheers.
  •  07-09-2006, 9:27 PM 19425 in reply to 19423

    Re: Full Name Validation - opening a can of worms ?

    masster:
    You are both right. So now I try a new (and final) approach, I know it is not optimum, but it will serve for my purposes. So I have one field named "Client Name" which includes both person names and company names. Again, plz don't jump on me :) for this idea. So my Q is: how do i have to modify this reg. exp. ^([0-9A-Z '-.]+)$ in order to have any string of names in uppercase, but without leading/trailing spaces and 'space' character should only have one succesive repetition ? Valid names: ALBERT EINSTEIN 2000 LTD. ALB-ERT EINSTEIN WITH MANY SURNAMES FOLLOWING 3M INvalid names: albert EinsTein (space)Albert(space)(space)Einstein(space) Hope it's clear. I have a headache. Cheers.


    you may try this:

        ^(?:[A-Z0-9'-.]+\s?)+(?<=\S)$

    there are two parts:

    (?:[A-Z0-9'-.]+\s?)+ : at least one set of strings formed by the  character-set[A-Z0-9'-.] and a possible trailing whitesapce. this construct can also guarentee no leading whitespaces and no repeating whitespaces.
    (?<=\S)$  :  the last character is not a whitspace.

    BTW. this validates also a single uppercase letter or a number, like: '3' and 'M'...

    Good luck,
    Xicheng

    perl -le 'print"So~*kde~box*DS*Zoxf*fe|er"^$\x23'
Page 1 of 2 (27 items)   1 2 Next >
View as RSS news feed in XML