Zend_Validate_Regex - allow only letters, numbers, and characters -_
So, I have a text field that can contain only letters, numbers, hyphens, dots and underscores. I would like to validate it using Zend_Validate_Regex but this pattern does not work. Why?
/[a-z][A-Z][0-9]-_./
Here is my text element:
$titleSlug = new Zend_Form_Element_Text('title_slug', array(
'label' => 'Title Slug开发者_开发技巧',
'required' => FALSE,
'filters' => array(
'StringTrim',
'Null'
),
'validators' => array(
array('StringLength', FALSE, array(3, 255)),
array('Regex', FALSE, array('pattern' => '/[a-z][A-Z][0-9]-_./'))
)
));
Your regex matches a string that contains a lowercase letter, an uppercase letter, a digit, a dash, an underscore and any other character, in that order. You need this:
/^[\w.-]*$/
^
and $
anchor the match at the start and end of the string.
\w
matches letters, digits and underscore; together with the dot and dash they form a character class ([...]
) which is repeated zero or more times (*
).
how about this:
/[a-zA-Z]*|\d*|-*|\.*|_*/
精彩评论