-2

I'm trying to get these regex criteria working but they seem to accept any input.

if(preg_match('/[A-Za-z]{3,30}$/', $FirstName)){
        if(preg_match('/[A-Za-z]{3,30}$/', $LastName)){

The system should reject any input that is not alphabetic or that is long than 30 characters / shorter than 3 characters.

Lucy Day
  • 3
  • 1
  • 2
  • 4
    already answered a 1000 times -> http://stackoverflow.com/questions/2385701/regular-expression-for-first-and-last-name – Doktor OSwaldo Mar 07 '16 at 14:40
  • The only valid RegExp for parsing names is `/.*/` but you've got to remember it's not even mandatory to have forename and surname - it's quite legal to change your name to an unpronounceable symbol if you like : https://en.wikipedia.org/wiki/Prince_%28musician%29 – CD001 Mar 07 '16 at 15:03

1 Answers1

0

preg_match("/[a-zA-Z]{3,30}$/", "", $FirstName)

preg_match("/[a-zA-Z]{3,30}$/", "", $LastName)

Should do the trick

Sudoscience
  • 301
  • 3
  • 11
  • I'm getting the error "Warning: preg_match(): Delimiter must not be alphanumeric or backslash". Any suggestions? x – Lucy Day Mar 07 '16 at 14:51
  • Just make it: `preg_match("/[a-zA-Z]{3,30}$/", "", $firstName)` instead – Sudoscience Mar 07 '16 at 14:59
  • Hi Brian O'Driscoll, meet `/^[a-z ,.'-]+{3,30}$/i` and Hi 周潤發, meet `/^[\p{L}'][ \p{L}'-]*[\p{L}]$/u` – Sudoscience Mar 07 '16 at 15:11
  • 1
    Your answer isn't wrong (based on the criteria of the question) - the question is working under a false premise in the first place : http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/ - 3-30 alphanumeric characters only is not going to match all valid forenames and surnames. – CD001 Mar 07 '16 at 15:13
  • Yeah I saw your comment above, I think an answer that is 'technically' correct based on the in-correctness of the question is still acceptable _;)_ – Sudoscience Mar 07 '16 at 15:15