zend validator regex
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' =>开发者_JAVA百科 '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
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*$'
You need add symbols for start and end regexp. For example:
->addValidator('regex',true,array('#^\\d{1,3}([,]\\d{1,3})*$#'))
true you need delimiters. but don't escape your slashes :)
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}/'));
精彩评论