Regex 如何在CakePHP中验证10位电话号码(仅限数字和空格)?
我希望验证一个10位数的电话号码,并在CakePHP中允许每个数字段之间最多有四个空格。我该怎么做 有效输入的示例: 04 123 123 34 0266 234 234 0266123123 02 66 55 12 12Regex 如何在CakePHP中验证10位电话号码(仅限数字和空格)?,regex,validation,cakephp,Regex,Validation,Cakephp,我希望验证一个10位数的电话号码,并在CakePHP中允许每个数字段之间最多有四个空格。我该怎么做 有效输入的示例: 04 123 123 34 0266 234 234 0266123123 02 66 55 12 12 这个验证对我很有效——希望这个答案能帮助其他人。如果你有改进,请张贴 'phone' => array( 'phone-1' => array( 'required' => true, 'allowEmpty'
这个验证对我很有效——希望这个答案能帮助其他人。如果你有改进,请张贴
'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)'
)
),
我想这将是确定的张贴一个问题,并回答自己作为参考其他基于。。。但是反对票不断流入:谢谢@michael petrotta的编辑。正则表达式可以是/^\d?\d{9}$/。。。但如果它有助于可读性,我更喜欢长篇大论。