Email validation issue

Hi,

I have a simple bit of HTML / Javascript to validate email addresses entered into a field, split by a comma.

<html>
<body>
<form name="myForm" onsubmit="return chkEmail()" method="post">
e: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
</body>
</html>
function validateEmail(email) {
    var re = /^(([^<>()[\\]\\\\.,;:\\s@\\"]+(\\.[^<>()[\\]\\\\.,;:\\s@\\"]+)*)|(\\".+\\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}

function chkEmail(){
	var myString = document.forms["myForm"]["fname"].value;
	var mySplitResult = myString.split(",");
	for(i = 0; i < mySplitResult.length; i++){
		emailCheck = validateEmail(mySplitResult[i]);
		alert(mySplitResult[i] + " - " + emailCheck);
		if (emailCheck = 'false') {
			alert(emailCheck);
		}
	}
}

If I enter e.g. “you@me.com,this@that.com” into the form, the alert debugs show:

First: you@me.com - true
Second: false
Third: this@that.com - true
Fourth: false

The first debug seems to show that the condition is true, but then the if statement to check for a “false” value is firing, showing the value of “emailCheck” = “false”, even though on the preceding alert, the value = “true”.

I’m not sure what’s going wrong.

Apologies for any silly mistakes.

Thanks

Here you are assigning the value of ‘false’ to the variable emailCheck

if (emailCheck = 'false') {

Since the assignment successfully occurred, the condition evaluates to true.

If you want to compare them, you should use === instead.


if (emailCheck === 'false') {