Hi

I'm not a fan of RegEx because I'm useless with it.

I am validating form fields. The first challenge that involves RegEx is to check that the "name" field contains only letters and optional spaces.

e.g. valid values:
John
John Smith
John David Smith

e.g. invalid value:
123
Johnny 5
John Smith 3rd

Here's my attempt at Regex, where $value is the raw POSTed field data:

if (preg_match("/[[a-zA-Z]*[ \t]*]*/",$value)) {
	$valid = True;
} else {
	$valid = False;
}

I believe that RegEx says I want any number of alphas in upper or lower case, followed by any number of spaces......and that pattern can repeat any number of times.

However, this is allowing numbers through. 😕

    names could also contain quote mark or hyphen, and what about non ascii letters ?

      dagon;10963544 wrote:

      names could also contain quote mark or hyphen, and what about non ascii letters ?

      I guess those are all valid points.
      I'm not after a perfect solution, so the requirements I set out above is probably enough for us.
      If I decide to open it up to handle other characters, then this will still have been a useful training exercise.

        By no means this is exhaustive, but it'll get you funny names like "José González", "Jean le Rond d'Alembert", "Jean-Jacques Rousseau" or "Jürgen Habermas".

        $valid = (preg_match("#(*UTF8)^[[:alpha:]'-\s]+$#", $input)) ? "1" : "0";
        
        // then...
        
        if ($valid) {
          // trim(), mysql_real_escape_string(), some other cleaning like removing more than one space between words etc.
        } else {
          // warnings here
        }

        Don't believe me. Test it.

          Write a Reply...