使用 zend 框架进行电话号码验证
我正在使用下面的代码在我的项目中通过电话号码进行验证,
$phone = new Zend_Form_Element_Text('phone', array(
'label' => $view->_('phone'),
'value' => '',
'class' => 'text-size text hastip',
'title' => $qtips_messages['key_phone'],
'required' => true,
'tabindex' => '13',
'validators' => array(
array('Digits', false, array(
'messages' => array(
'notDigits' => "Phone Invalid Digits, ex. 1234567890",
'digitsStringEmpty' => "",
))),
array('notEmpty', true, array(
'messages' => array(
'isEmpty' => 'Phone can\'t be empty'
)
)),
array('StringLength', false, array(10, 10, 'messages' => array(
'stringLengthInvalid' => "Phone Length Invalid entry",
'stringLengthTooShort' => "Phone Invalid Length , ex. 1234567890"
))),
),
'filters' => array('StringTrim'),
'decorators' => $this->requiredElementDecorators,
'description' => '<img src="'.$baseurl.'/images/star.png" alt="required" />',
'filters' => array('StringTrim')
));
$this->addElement($phone);
当字段为空、无效、长度时,这效果很好。我的要求是,电话字段应该接受空格,例如 123 456 7890
。 请就此提出建议。
I am using below code to validate by phone number in my project,
$phone = new Zend_Form_Element_Text('phone', array(
'label' => $view->_('phone'),
'value' => '',
'class' => 'text-size text hastip',
'title' => $qtips_messages['key_phone'],
'required' => true,
'tabindex' => '13',
'validators' => array(
array('Digits', false, array(
'messages' => array(
'notDigits' => "Phone Invalid Digits, ex. 1234567890",
'digitsStringEmpty' => "",
))),
array('notEmpty', true, array(
'messages' => array(
'isEmpty' => 'Phone can\'t be empty'
)
)),
array('StringLength', false, array(10, 10, 'messages' => array(
'stringLengthInvalid' => "Phone Length Invalid entry",
'stringLengthTooShort' => "Phone Invalid Length , ex. 1234567890"
))),
),
'filters' => array('StringTrim'),
'decorators' => $this->requiredElementDecorators,
'description' => '<img src="'.$baseurl.'/images/star.png" alt="required" />',
'filters' => array('StringTrim')
));
$this->addElement($phone);
This works well when field is empty,invalid,length. my requirement is ,phone field should accept spaces also like 123 456 7890
.
Kindly advice on this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太确定数字验证器如何处理空格,但我想它不会允许它们(或者您不会在这里发帖)。因此,不要使用数字验证器,而是将其更改为正则表达式验证器。我不擅长正则表达式,所以我建议您查看以下页面,其中包含电话号码的多个表达式,而不是给您表达式:用于电话号码验证的综合正则表达式
添加验证器应该类似于:
就像我说的,我不擅长正则表达式,因此正则表达式本身可能不完全准确,但添加这样的验证器应该可以解决您的问题。
I'm not too sure how the digits validator handles the spaces but I guess it won't allow them (or you wouldn't be posting here). So instead of using the digits validator, change it for a regex validator. I'm not good with regex so instead of me giving you the expression I suggest you take a look at the following page containing several expressions for phonenumbers: A comprehensive regex for phone number validation
Adding the validator should look something like:
Like I said, I'm no good with regex so the regex itself might not be completely accurate, but adding a validator like that should solve your problem.