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.
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.