I have registration form in which there are name, email, etc fields. In email field when I type ** email@yahoo.co ** it is accepted. But in real it is wrong because email@yahoo.co ** is not end with m. It should be **email@yahoo.com.
Is there any way to validate it, so that it only accept email which is ended with .com and in which there is @.
view/register:
<?php
echo $this->form()->openTag($form);
?>
<dl class="zend_form">
<dt><?php echo $this->formLabel($form->get('name')); ?></dt>
<dd><?php
echo $this->formElement($form->get('name'));
echo $this->formElementErrors($form->get('name'));
?></dd>
<dt><?php echo $this->formLabel($form->get('email')); ?></dt>
<dd><?php
echo $this->formElement($form->get('email'));
echo $this->formElementErrors($form->get('email'));
?></dd>
<dt><?php echo $this->formLabel($form->get('password')); ?></dt>
<dd><?php
echo $this->formElement($form->get('password'));
echo $this->formElementErrors($form->get('password'));
?></dd>
<dt><?php echo $this->formLabel($form->get('confirm_password')); ?></dt>
<dd><?php
echo $this->formElement($form->get('confirm_password'));
echo $this->formElementErrors($form->get('confirm_password'));
?></dd>
<br/>
<dd><?php
echo $this->formElement($form->get('submit'));
echo $this->formElementErrors($form->get('submit'));
?></dd>
</dl>
<?php echo $this->form()->closeTag() ?>
Thanks in advance
You can validate the form using a custom validator in which you allow domains from a specific whitelist (can be done using a callback validator for instance, even though a proper custom validator would be better).
It really depends on your use case though, but you could also validate using a blacklist :)
An interesting solution would probably be having an hidden field (checkbox or radiobutton), and first you validate against your blacklist using the keys of an array from your config, and pass the proper domain as a param for the error message...
Say your config contains the following
return [
'domains_blacklist' => [
'yahoo.co' => 'yahoo.com',
],
];
In a custom validator, check whether the email finished with one of these strings:
in_array($domain_from_value, array_keys($this->config['domains_blacklist'))
In case it does, return an error with a message like "Are you sure the domain you typed (%domain%) is valid?".
When you display the form again, make sure you tested whether the error exist, and when it exist just set the hidden field visible.
Finally, change the validator so when the checkbox is checked it does not validate against the list anymore (use a second param in the valid method, it is called context and contains all the fields from the form).