How can I perform alphanumeric with regex space model validation in CakePHP?

I have a user registration form and I want the user to enter their first and last name (i.e. Donalg Doonsberry). If the user does not provide their name in this way, entry will be rejected. I figured something like this would work for my regex user validation in cakephp:

  'name'=>array('Name has to be comprised of letters.
                '=>array('rule'=>array('custom', '/[A-Za-z ]+/')
           )),

      

0


a source to share


3 answers


You have to be careful when checking names ... how about:

  • Donalg McDonald <- capital in the middle of the word
  • Donalg Fooble-Whiffery> - portable
  • Donalg Dénnsbnérry-McDonald Sr. 3rd - you get the idea

To check names in the specified format:



/[A-Z][a-z]+ [A-Z][a-z]+/

      

Be a little softer:

/([\w.-]+ )+[\w+.-]/

      

+6


a source


Personally, I wouldn't bother checking the first / last name. You can only check their length (there is a built-in validation rule for that), and make sure you set "allowEmpty" => true if that's what you want.



0


a source


I am using below code to validate fields with alphaNumeric and spaces.

'rule1' = array(
   'rule' => array('custom','/([\w.-]+ )+[\w+.-]/'),
   'required' => true,
   'message' => 'Required field.'
)

      

I hope to help you.

0


a source







All Articles