使用 zend 框架进行电话号码验证

发布于 2024-12-27 23:00:35 字数 1525 浏览 2 评论 0原文

我正在使用下面的代码在我的项目中通过电话号码进行验证,

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦明 2025-01-03 23:00:35

我不太确定数字验证器如何处理空格,但我想它不会允许它们(或者您不会在这里发帖)。因此,不要使用数字验证器,而是将其更改为正则表达式验证器。我不擅长正则表达式,所以我建议您查看以下页面,其中包含电话号码的多个表达式,而不是给您表达式:用于电话号码验证的综合正则表达式

添加验证器应该类似于:

array('regex', false, array('/^[0-9 ]+$/'))

就像我说的,我不擅长正则表达式,因此正则表达式本身可能不完全准确,但添加这样的验证器应该可以解决您的问题。

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:

array('regex', false, array('/^[0-9 ]+$/'))

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文