RegEx Issue: Preg_Match Only 2 non consecutive spaces, no special characters [PHP] -


so class simulating putting in first, middle, , last name form. have variable $fullname concats them , pulls out middle initial strtolower , ucfirst...

so robert james smith parsed robert j smith.

now if put r!bert j smith (note special character"!") accepted. combat did if/else statement pregmatch.

if (preg_match("/[^a-z\s]/i",$fullname)) {         echo ("<span class = 'warning'>invalid user name!</span>"."<br>"                 ."please use characters a-z name."."<br>");         exit;      }     else {                            echo ("user name: ".$fullname."<br>");      }  

this says if there besides letters , whitespace, throw echo message.

now have run issue if enter r obert j smith. (note space character inbetween r , o). because allow whitespace says okay. goes on mess "create username , password" functions later on.

tldr: asking allow pregmatch match or not match 2 nonconsecutive whitespaces , characters a-z/i

note: tried doing various versions of \s {2} consecutive spaces, not non-consecutive spaces.

ps: there ever 2 spaces. trim input before parsed $fullname .

i have been trying 3 hours , frustration blocking ability approach in different direction.

thank you, thelaughingman

it possible regular expression:

/^\w+(\s\w+){0,2}$/ 

which matches 1, 2 or 3 space-separated words, or use match 3:

/^\w+\s\w+\s\w+$/ 

but split check invalid characters (/[^\w]/) off checking number of words entered:

if ( count( explode(' ', $fullname) ) != 3 ) 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -