在cakephp FOR INTEGER中验证

时间:2013-08-29 09:14:00

标签: cakephp-2.3

我实际上需要对电话号码进行整数验证并使用此代码,但它无法正常工作

'phone'=>$this->validator()->add(’phone’, array(
’required’ => array(
’rule’ => ’notEmpty’,
’required’ => ’create’
),
’size’ => array(
’rule’ => array(’between’, 8,20),
’message’ => ’phone should be at least 8 chars long’
)
));

2 个答案:

答案 0 :(得分:0)

这真的很适合我尝试一下,检查可能会帮助你太多

    'phone' => array(
    'phone-1' => array(
        'required'   => true,
        'allowEmpty' => false,
        'last'       => true,
        'rule'       => 'notEmpty',
        'message'    => 'This field cannot be left blank'
    ),
    'phone-2' => array(
        'last'       => true,
        'rule'       => array('minLength', 10),
        'message'    => 'This field has a minimum length of 10 characters'
    ),
    'phone-3' => array(
        'last'       => true,
        'rule'       => array('maxLength', 14),
        'message'    => 'This field has a maximum length of 14 characters'
    ),
    'phone-4' => array(
        'last'       => true,
        'rule'       => array('phone', '/^[0-9]( ?[0-9]){8} ?[0-9]$/'), 
        // start and end in a digit and require 8 digits inbetween (so total of 10 digits required).
        // allow a single space between any digit (so this is valid "0 1 2 3 4 5 6 7 8 9" ... 19 characters ... but our minLength and maxLength validation protects us from this)
        // minLength 10 means "0123456789" is the shortest valid input.
        // maxLength 14 means "01 23 45 67 89" is the most spaced we can be (i.e. max 4 spaces allowed... they could be anywhere in the string e.g. "12 123 123 1 1") 
        // http://en.wikipedia.org/wiki/Telephone_numbers_in_Australia
        'message'    => 'Please enter a valid 10 digit phone number (digits and spaces only)'
    )
),

答案 1 :(得分:0)

static Validation::phone(mixed $check, string $regex = null, string $country = 'all')

手机验证美国电话号码。如果要验证非美国电话号码,可以提供正则表达式作为第二个参数,以涵盖其他数字格式。

public $validate = array(
    'phone' => array(
        'rule' => array('phone', null, 'us')
    )
);