简体   繁体   中英

Laravel regular expression whitespace

$rules = array(
  'password' => array(
                  'required',
                  'min:6',
                  'regex:/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*(_|[^\w])).+$/')
);

I am using this regular expression to allow (Contain at least one uppercase/lowercase letters and one number)

but I need help to validate that have only one whitespace (so continue whitespace makes invalid)

Add a (?!\\S*\\s\\S*\\s) which prohibits more than one whitespace (it allows 0 or one whitespace in the password).

'regex:/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*(_|[^\w]))(?!\S*\s\S*\s).+$/'

If you want to force exactly one whitespace you'd do (?=\\S*\\s\\S*$) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM