douludi8413 2015-11-19 06:22
浏览 49

电子邮件以2种格式接受,即abc.def和abc.def@testdomain.com

I need emails to be validated as a EMAIL(abc.def@testdomain.com) and at the same time as a USERNAME (abc.def). I have used Yii EMAIL Validation which is strict validation but i have not to change it because it is using in many other places in the project.

However, i need to add CUSTOM USERNAME VALIDATION along with YII EMAIL VALIDATION for a single MODEL.

Below is the Code to do that.

public function rules() {
    $rules = array(
        array('email', 'required', 'message'=>'Please complete {attribute}'),
        array('email', 'EmailCustom'),
        array('email', 'unique')
    );
}

Whereas EmailCustom Validator Class is:

class EmailCustom extends CEmailValidator
{
    public $pattern = '/^[ ]*[a-zA-Z0-9!#$%&\'*+\\/=?^_`’{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`’{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?[ ]*$/';

    protected function validateAttribute($object, $attribute)
    {
        $pattern = '/^[ ]*[a-zA-Z0-9!#$%&\'*+\\/=?^_`’{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`’{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?[ ]*$/';
        if ($object->visitor_card_status == 2) {
            if (!preg_match($pattern, $object->$attribute)) {
                if ($object->$attribute !== $object->first_name . '.' . $object->last_name) {
                    $this->addError($object, $attribute, 'Email is incorrect format');
                }
            }
        } else {
            if (!preg_match($pattern, $object->$attribute)) {
                $this->addError($object, $attribute, 'Email is incorrect format');
            }
        }
    }

    public function clientValidateAttribute($object, $attribute)
    {
        if ($this->validateIDN)
        {
            Yii::app()->getClientScript()->registerCoreScript('punycode');
            // punycode.js works only with the domains - so we have to extract it before punycoding
            $validateIDN='
                            var info = value.match(/^(.[^@]+)@(.+)$/);
                            if (info)
                                value = info[1] + "@" + punycode.toASCII(info[2]);
                            ';
        } else {
            $validateIDN = '';
        }

        $message = $this->message!==null ? $this->message : Yii::t('yii','{attribute} is not in a recognised format. <span style="text-transform:capitalize;">Please </span>revise.');
        $message = strtr($message, array(
            '{attribute}'=>$object->getAttributeLabel($attribute),
        ));
}

So, What should be added in the pattern of Validator Class to accept only two FORMATS: abc.def and abc.def@testdomain.com

Hope to hear from you soon. Thanks

  • 写回答

1条回答 默认 最新

  • du5910 2015-11-19 09:41
    关注

    Actually, I have updated the pattern which accepts both the formats and using the Yii Validator function does the trick.

    Below is the pattern to accept both the format. i.e. abc.def and string@testdomain.com

    /(^[ ]*[a-zA-Z0-9!#$%&\'*+\\/=?^_`’{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`’{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?[ ]*$)|([a-zA-Z0-9]+\.[a-zA-Z0-9]+)/
    

    I hope it may help others. Thanks

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP