Phone number validation using zend framwork - php

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.

Related

CakePHP Number Validation

Is there any cakephp validation for numbers only. That input field should only accept Numbers whether its float OR integer.
I have this field validation in model, But I don't know what rule to use.
'payment_commission' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Required.'
)
)
Any help is much appreciated..
You want to use numeric to test for valid numbers:-
'payment_commission' => array(
'required' => array(
'rule' => 'notEmpty', // use notBlank as of CakePHP 2.7
'message' => 'Required.'
),
'numeric' => array(
'rule' => 'numeric',
'message' => 'Numbers only'
)
)
You can alternatively use decimal to check for floats or naturalNumber for natural numbers. Full details can be found in the offical docs.
You can also make sure that the input field only allows for numeric values:-
<?= $this->Form->input('payment_commission', ['type' => 'number']) ?>
This has the added advantage that on some mobile devices the keyboard will switch to a number pad for easier input. It's important to ensure that the server-side validation (the Cake part) is in place though as there are ways for people to get round client-side validation.
You can try below cakePHP code for number validation.
'payment_commission' => array(
'numeric' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Required.'
)
)
)
You can use below html pattern code in for your textbox.
<input type="text" name="payment_commission" pattern="[0-9]{10}" title="Three letter country code">
Here you can manage your pattern by changing [0-9]{10}. ([0-9] = expected integers from User, {10} = length).
You can use following rule to validate numbers(float and integer):
'payment_commission' => array(
'numeric' => array(
'rule' => array('decimal', 2),
'message' => 'Please enter only numbers',
'allowEmpty' => false,
'required' => true,
)
),
Let me know if this works for you.

Zend Form Validate URL

I am currently validating a URL using the Regex Pattern and it appears to be working correctly. However, if I leave the URL field blank, it should not check the Regex Validation or perhaps just return a message like "No URL given".
Here is an example of my current code I'm working with:
array(
'name' => 'programurl1',
'attributes' => array(
'type' => 'text',
'error_msg' => 'Enter Valid Program URL 1',
'label_msg' => 'Program URL 1 *'
),
'validation' => array(
'required' => true,
'filters' => array(
array(
'name' => 'StripTags'
),
array(
'name' => 'StringTrim'
)
),
'validators' => array(
array(
'name' => 'Regex',
'options' => array(
'pattern' => '/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/'
)
)
)
)
)
I'm not certain how to accomplish what I am looking for when the URL field is blank.
Instead of a type text, you can use the url type. That is specifically meant to enter url values:
$this->add(array(
'name' => 'programurl',
'type' => 'Zend\Form\Element\Url',
'options' => array(
'label' => 'Program URL 1'
),
'attributes' => array(
'required' => 'required'
)
));
The url element is a special HTML5 element, see also the docs.
Zend\Form\Element\Url is meant to be paired with the Zend\Form\View\Helper\FormUrl for HTML5 inputs with type url. This element adds filters and a Zend\Validator\Uri validator to it’s input filter specification for validating HTML5 URL input values on the server.
Afaik if the browser cannot render the url input element, it just shows the text input as a fallback.

How do I use ZF2 regex validators within the factory pattern

Is anyone familiar with the use of ZF2 regex validators within the factory pattern?
I have taken this code from various blogs and other stackoverflow questions, but it does not seem to work.
The addition of the regex validator blocks all changes to my form from updating the database - so the validator must be failing even when I insert a number.
However, when I check
$form -> getMessages();
I get an empty array. Any insight would be appreciated.
To illustrate I use a very simple regex that, as I understand it, would block any entry character that is not a number.
$inputFilter->add($factory->createInput(array(
'name' => 'Number',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'max' => 20,
),
),
),
array(
'name' => 'Regex',
'options' => array(
'pattern' => '/^[0-9]+$',
'messages' => array(
'Invalid input, only 0-9 characters allowed'
),
),
),
)));
At a glance, Regex validator should sit inside "validators" array...

cakephp show validation errors with jquery

I'm using cakephp 2.0 and I'm having problem using Cake's validation thing, which works wonderful when used for it alone.
var $validate = array(
'loginname' => array(
'minCharactersRule' => array(
'rule' => array('minLength', 3),
'required' => true,
'allowEmpty' => false,
'on' => 'create',
),
'alphaNumericRule' => array(
'rule' => 'alphaNumeric',
),
'uniqueRule' => array(
'rule' => 'isUnique',
),
),
'password' => array(
'minCharactersRule' => array(
'rule' => array('minLength', 5),
'required' => true,
'allowEmpty' => false,
),
// on so on ...
Due to Internalization I put the error messages into the view like this:
echo $this->Form->input('display_name', array(
// some other options ...
'error' => array(
'betweenRule' => __('Your Dispay Name must contain at least 3 characters and should be maximal 20 characters long.'),
'uniqueRule' => __('This Display Name is already in use!'),
),
);
Now I want jQuery to validate the form on client-side as well with some little - sexy message box to return validation errors if something goes wrong.
The Ajax Request works fine but the problem here is: I only get returned the name of the validation rule, not the message.
sure: Because I didn't specified any messages in the model.
But how can I get those messages I declared in the view?
Is there any solution without having to type everything twice (PHP and JS)?

How to give validator for floating point number in zend form?

Following is the piece of code which has been taken from form,
$debit_amount = new Zend_Form_Element_Text('debit_amount', array(
'label' => 'Debit_amount',
'value' => '',
'class' => 'text-size text',
'required' => true,
'tabindex' => '13',
'validators' => array(
array('Digits', false, array(
'messages' => array(
'notDigits' => "Invalid entry, ex. 10.00",
'digitsStringEmpty' => "",
))),
array('notEmpty', true, array(
'messages' => array(
'isEmpty' => 'Debit_amount can\'t be empty'
)
)),
),
'filters' => array('StringTrim'),
//'decorators' => $this->requiredElementDecorators,
//'description' => '<img src="' . $baseurl . '/images/star.png" alt="required" />',
));
$this->addElement($debit_amount);
When i try submit the form using floating number , it throws my error "Invalid Enter....". It does not validate floating point number. Kindly advice.
Use Zend_Validate_Float:
'validators' => array(
array('Float', ...
You are getting the error message because the Digits validator only validates digits. There is a comment about it in the Zend_Validate documentation:
Note: Validating numbers
When you want to validate numbers or numeric values, be aware that this validator only
validates digits. This means that any other sign like a thousand separator or a comma
will not pass this validator. In this case you should use Zend_Validate_Int or
Zend_Validate_Float.
As #Zyava says, using Zend_Validate_Float is the way to go.

Categories