1

我正在尝试在这样的 zend 表单元素上使用正则表达式验证器-

    $textarea = $this->createElement('text','scores');

    $textarea->setLabel('Enter a comma separated list of numbers');

    $textarea->setDecorators(
            array('ViewHelper',
                array('HtmlTag', 
                    array('tag' => 'div',
                          'class'=>'scores'
                    )
                )
            )
    );
    $textarea->addDecorator('Label')
        ->setRequired(true)
        ->addFilter(new Zend_Filter_StringTrim())
        ->addValidator('regex',true,array('^\d{1,3}([,]\d{1,3})*$'))
        ->addErrorMessage('Please enter a comma separated list of numbers');

我只是想验证文本区域是否包含逗号分隔的数字列表。

目前我收到“使用模式 '^\d{1,3}([,]\d{1,3})*$' 时出现内部错误”。

我猜正则表达式有问题?

任何帮助,将不胜感激 :)

谢谢,皮特

4

4 回答 4

0

Try escaping the backslashes:

'^\\d{1,3}(,\\d{1,3})*$'

You don't need the brackets around the comma.

Also, you might want to allow whitespace between the numbers and separators:

'^\\s*\\d{1,3}(\\s*,\\s*\\d{1,3})*\\s*$'
于 2011-03-15T07:19:16.550 回答
0

您需要为开始和结束正则表达式添加符号。例如:

->addValidator('regex',true,array('#^\\d{1,3}([,]\\d{1,3})*$#'))
于 2012-11-03T19:52:17.253 回答
0

恕我直言,您在正则表达式末尾缺少斜杠“/”。我不是专家,但这对我有用: ->addValidator(new Zend_Validate_Regex('/^[a-zA-Z0-9][a-zA-Z0-9 ._-]{1,31}/'));

于 2014-03-16T07:03:15.607 回答
0

真的,你需要分隔符。但不要逃避你的斜线:)

于 2012-11-27T08:40:07.360 回答