Validation and regular expressions

I work in cakephp1.3. I have to check my form with cakephp validation in model.i added a regular expression to limit the input of special characters.

My code is written below:

var $validate = array( 'name' => array( 'unique'=>array( 'rule' => 'isUnique', 'message' => 'Already taken', ), 'pattern'=>array( 'rule' => '/^[az]$/i', 'message' => 'Only letters allowed', ), 'empty'=>array( 'rule' => 'notEmpty', 'message' => 'This field cannot be left blank', )) ); 

the 'unique' rule does not work. it worked fine before adding the "pattern" rule to the script. What is the problem? I am trying to arrange the order of the rules mentioned above, I thought there might be some kind of positioning problem, but nothing happened.

plz help me ..

+4
source share
2 answers

Replace $ with +:

 var $validate = array( 'name' => array( 'notempty' => array( 'rule' => array('notempty'), 'message' => 'This field cannot be left blank', ), 'unique'=>array( 'rule' => 'isUnique', 'message' => 'Already taken' ), 'pattern'=>array( 'rule' => '[a-zA-Z]+', 'message' => 'Only letters allowed', ), ), ); 
+4
source

You need to clarify how long your char can be. Add this data after "]":

  • * for 0 or more
  • + for 1 or more
  • ? for 0 or 1
  • {2.9} for account number between limits
  • {2}} at the minimum value
  • {0,1024} for maximum only

http://www.php.net/manual/en/regexp.reference.meta.php

examples:

  'rule' => '/^[az]+$/i', 'rule' => '/^[az]{3-255}$/i', 
+1
source

All Articles