multiple emails validatiion with text box

i have a textbox where user can enter multiple email with comma separated so how i can chaeck each email is valid or not

You have to create a custom ad hoc validator. I made one just for you.

All of it goes in your model.




use yii\validators\EmailValidator;


public function rules() {

    	return [

        	['email', 'checkEmailList'],

            	// other rules

    	];

	}

//note where it says this email change that to whatever your field is.  you can't use $attribute because it doesn't return the right value

	public function checkEmailList($attribute, $params) {

    	$emails = $this->email;

    	//convert email list to string to an array so we can loop through it using ";"  as a delimiter.

    	//if it is in an array do nothing i.e. using somthing like select2

    	if (is_array($emails)) {

        	$emails = explode(';', $emails);

    	}

    	//declair email validator

    	$validator = new EmailValidator;

    	foreach ($emails as $email) {

        	if (!$validator->validate($email)) {

            	$this->addError($attribute, "'.$email.' is not a valid email.");

        	}

    	}

	}



and a shorthand version of the same thing (while still being readable, tech we can remove one line) would be




public function checkEmailList($attribute, $params) {

    	$validator = new EmailValidator;

    	$emails = is_array($this->email)? : explode(';', $this->email);

    	foreach ($emails as $email) {

        	$validator->validate($email)? : $this->addError($attribute, "{email} is not a valid email.");

    	}

}



Careful! This could turn addresses valid that have been invalid before. Btw, spaces can be legit parts of email addresses.

This is written in RFC 53522 standard but almost everyone (including Yii) is taking more practical approach and using this regex http://www.regular-expressions.info/email.html or something similar.

Hm, I actually think spaces were allowed in local parts long before RFC 5322: RFC 2822 already allowed the local part to be a quoted string. That part of an email address is officially allowed to contain all kinds of characters you would never expect.

@da:sourcerer good catch… i updated it. I didn’t mean to remove all white space i wanted to remove just white space before and after each email and wasn’t thinking.

I think the OP should trim all of the emails though before he saves them just in case he is trying to email to someemail@something.com and it is saved as spaceSomeemai@something.com it might get sent and count the space as part of the email.

I guess the real thing is that if the Yii validator says it’s a valid email with spaces or no spaces let it through.

It also looks like there is also an each validator coming out in 2.04 you might be able to use.

I also updated it to use select2 i.e. check if it is already in an array.

Well, it isn’t easy to do in the first place. I thought about splitting by [font=“Courier New”]/\s*,\s*/[/font]. But then I realised this, too, could be a valid part of an email address. I think it’s a mistake to allow for such an input in the first place instead of providing a set f input fields.

Absolutely. This is the kind of thing enabling rather subtle errors in sorting and processing.

Hm, sounds expensive if it is done right. i.e. with backtracking and so on ::)

thanks skworden for your help.thanks guys ,

i m sorry but i did not get where you are removing whitespaces from user input.i m exploding this with comma so i think i have to change your code little bit ,with this ; to ,)

what where you guys talking about whitespaces ,and where you have updated code

I removed it. It was




/remove all white spaces

$emails = preg_replace('/\s+/', '', $this->email);



It works as is for just taking a string of emails separated with a ;. It also throws an error the the specific email not just the whole string saying it’s invalid for better user experience.

Hopefully you get this to work how you need it to.

yes i m using this and it is working ,thnku