1

I'm trying to use a regex validator on a zend form element like this-

    $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');

I'm just trying to validate that the text area contains a list of comma separated numbers.

Currently im getting "Internal error while using the pattern '^\d{1,3}([,]\d{1,3})*$'".

I guess there's something wrong with the regex?

Any help would be appreciated :)

thanks, pete

PeterL
  • 175
  • 5
  • 15

4 Answers4

0

You need add symbols for start and end regexp. For example:

->addValidator('regex',true,array('#^\\d{1,3}([,]\\d{1,3})*$#'))
vedmed
  • 255
  • 2
  • 7
  • 16
0

true you need delimiters. but don't escape your slashes :)

user1743741
  • 134
  • 1
  • 6
0

IMHO you are missing slash "/" at the end of your regex. I'm not an expert but this is working for me: ->addValidator(new Zend_Validate_Regex('/^[a-zA-Z0-9][a-zA-Z0-9 ._-]{1,31}/'));

Enriqe
  • 567
  • 1
  • 6
  • 22
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*$'
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561