Welcome to the Cookbook

loading...

4.1.5.1 Custom Regular Expression Validation

If the validation technique you need to use can be completed by using regular expression matching, you can define a custom expression as a field validation rule.

var $validate = array(
    'login' => array(
        'rule' => '/^[a-z0-9]{3,}$/i',  
        'message' => 'Only letters and integers, min 3 characters'
    )
);
  1. var $validate = array(
  2. 'login' => array(
  3. 'rule' => '/^[a-z0-9]{3,}$/i',
  4. 'message' => 'Only letters and integers, min 3 characters'
  5. )
  6. );

The example above checks if the login contains only letters and integers, with a minimum of three characters.

The regular expression in the rule must be delimited by slashes. The optional trailing 'i' after the last slash means the reg-exp is case insensitive.