How to add onblur/onfocus event to this email validation?

I need some help.

I’m trying to translate this onblur/onlick event to my email validation:


onblur="if(this.value == '') { this.value='CONFIRM YOUR EMAIL'}" onfocus="if (this.value == 'CONFIRM YOUR EMAIL') {this.value=''}"

Needs to be added to each of these:


function checkEmail(){
	email = document.getElementById("emailAddress").value;
	confirm_email = document.getElementById("confirm_email").value;
			
	if(checkEmpty(email)==true){
		document.getElementById("emailAddress").style.color = 'red';
		document.getElementById("emailAddress").value = "ENTER YOUR EMAIL";
		return false;
	}
	else if(checkValidEmail(email)==false){
		document.getElementById("emailAddress").style.color = 'red';
		document.getElementById("emailAddress").value = "PLEASE ENTER VALID EMAIL ADDRESS";
		return false;
	}
	else if(email!=confirm_email){
		document.getElementById("confirm_email").style.color = 'red';
		document.getElementById("confirm_email").value = "EMAIL ADDRESS DO NOT MATCH";
		return false;
	}

Any suggestions?

Hi,

Welcome to the forums :slight_smile:

Can you post your markup?


<script>

//Email Validation
function checkEmail(){
	email = document.getElementById("emailAddress").value;
	confirm_email = document.getElementById("confirm_email").value;
			
	if(checkEmpty(email)==true){
		document.getElementById("emailAddress").style.color = 'red';
		document.getElementById("emailAddress").value = "ENTER YOUR EMAIL";
		return false;
	}
	else if(checkValidEmail(email)==false){
		document.getElementById("emailAddress").style.color = 'red';
		document.getElementById("emailAddress").value = "PLEASE ENTER VALID EMAIL ADDRESS";
		return false;
	}
	else if(email!=confirm_email){
		document.getElementById("confirm_email").style.color = 'red';
		document.getElementById("confirm_email").value = "EMAIL ADDRESS DO NOT MATCH";
		return false;
	}


</script>


<form name="coach_campaign" id="coach_campaign" onsubmit="return checkEmail();" action="signup.php" method="get" target="hidden_iframe">
			<input type="hidden" name="catalogId" value="10051" />
			<input type="text" id="emailAddress" name="emailAddress" maxlength="50" size="30" value="ENTER YOUR EMAIL" style="margin-top:150px;" onblur="if(this.value == '') { this.value='ENTER YOUR EMAIL'}" onfocus="if (this.value == 'ENTER YOUR EMAIL') {this.value=''}"/>
			<input type="text" id="confirm_email" name="confirm_email" maxlength="50" size="30" value="CONFIRM YOUR EMAIL" onblur="if(this.value == '') { this.value='CONFIRM YOUR EMAIL'}" onfocus="if (this.value == 'CONFIRM YOUR EMAIL') {this.value=''}"/>
			<input type="hidden" name="langId" value="-1" />
			<input type="hidden" name="mode" value="M7" />
			<input type="hidden" name="marketingSource" value="FPENDSALE" />
			<input type="hidden" name="requestPreferences" value="Y" />
			<input type="hidden" name="storeId" value="10551" />
			<input type="submit" value="SUBMIT"  id="submit_email" onClick="" />
		</form>

Ah, ok. So you’re wanting the validation to take place when the input field loses focus?

Yes. Basically I want the “ENTER YOUR EMAIL”, “CONFIRM YOUR EMAIL”, “EMAIL ADDRESS DO NOT MATCH”, and “PLEASE ENTER VALID EMAIL ADDRESS” etc… to disappear when the user clicks in the text field after seeing the message. I got it to work for the first two but third and fourth one sticks around. So I’m trying to figure a way to make it work for any message that shows up in the text field. Makes sense?

Can we approach this from another direction?

You have a text input where the user should enter an email address and a second text input where they should confirm it.
When the user presses submit, the form should be validated so that:

  • Neither field may be blank
  • Both fields must match
  • Both fields must contain a valid email address

Is that correct?

Sorry if this seems like a step backwards, as opposed to forwards, but there is method to my madness :slight_smile:

Just to clarify the validation already works. It’s just the “EMAIL ADDRESS DO NOT MATCH”, and “PLEASE ENTER VALID EMAIL ADDRESS” won’t disappear when the user clicks in the text field to enter a valid email address. The user has to physically delete the “EMAIL ADDRESS DO NOT MATCH”, and “PLEASE ENTER VALID EMAIL ADDRESS” messages to re-enter their email addresses in the text fields. It already works for “ENTER YOUR EMAIL” and “CONFIRM YOUR EMAIL” since I have the onblur and onfocus applied to those text fields. I want the onblur and onfocus to be applied universally.

Hi,

That’s good :slight_smile:

I was just wondering if there was any reason that you want the error message to appear as the value of the input.
If a user mis-types their confirmation email address, then you would overwrite what they have entered with an error message. This isn’t very user friendly.
How about displaying it next to the field respective field instead?

It’s not my call unfortunately. Can you help?

Sure :slight_smile:

Are you allowed to alter the JavaScript and the HTML?

The reason I ask is that the code you posted is full of bad practices and it would be nice to tidy this up a bit.

Yes. Any way to clean it up would be great.

Now we’re talking.

So, let’s start with the form. I’m going to start by removing the inline stuff (CSS / event handlers):

<form name="coach_campaign" id="coach_campaign" action="signup.php" method="get" target="hidden_iframe">
    <input type="hidden" name="catalogId" value="10051" />
    <input type="text" id="emailAddress" name="emailAddress" maxlength="50" value="ENTER YOUR EMAIL" />
    <input type="text" id="confirm_email" name="confirm_email" maxlength="50" value="CONFIRM YOUR EMAIL" />
    <input type="hidden" name="langId" value="-1" />
    <input type="hidden" name="mode" value="M7" />
    <input type="hidden" name="marketingSource" value="FPENDSALE" />
    <input type="hidden" name="requestPreferences" value="Y" />
    <input type="hidden" name="storeId" value="10551" />
    <input type="submit" value="SUBMIT" id="submit_email" />
</form>

Now, let’s get a reference to the form and its fields, then attach the submit handler unobtrusively:

var form = document.getElementById("coach_campaign"),
    emailField = document.getElementById("emailAddress"),
    confirmationField = document.getElementById("confirm_email");

form.addEventListener("submit", checkEmail, false);

Now we need to work out how to validate the fields and display the errors:

function checkEmail() {
  var email = emailField.value,
      confirmation = confirmationField.value;
  
  if(checkEmpty(email)){
    emailField.classList.add("invalid");
	return false;
  } else if(!checkValidEmail(email)){
    emailField.classList.add("invalid");
    emailField.value = "PLEASE ENTER VALID EMAIL ADDRESS";
	return false;
  } else if(email !== confirmation){
    emailField.classList.add("invalid");
	confirmationField.value = "EMAIL ADDRESS DO NOT MATCH";
	return false;
  }
}

Again, I’m using a class, rather than inline styles.

Now all we need to do is to add the requested focus/blur functionality:

emailField.addEventListener("focus", clearValue, false);
emailField.addEventListener("blur", setValue, false);
confirmationField.addEventListener("focus", clearValue, false);
confirmationField.addEventListener("blur", setValue, false);

and:

function clearValue(){
  this.setAttribute("prev-value", this.value);    
  this.value = "";
}

function setValue(){
  if (this.value === ""){
      this.value = this.getAttribute("prev-value");
  }
}

Here’s a complete demo.

This could probably be tightened up some more (as it is a little verbose), but for purposes of this demonstration I will leave it as it is.

I hope this helps.

Thank you Pullo for your help. I noticed that if I just enter just an incomplete email in either box it doesn’t tell me to enter a valid email. The enter a valid email address only shows up when I go back to the page after I hit submit.

Yeah, I stubbed out the methods that you said were working.
You’ll have to add them to my code.

I changed the submit handler slightly, as the form seemed to be submitting when it was invalid:

function checkEmail() {
  var email = emailField.value,
      confirmation = confirmationField.value,
      formValid = true;
  
  if(checkEmpty(email)){
    emailField.classList.add("invalid");
	formValid = false;
  } else if(!checkValidEmail(email)){
    emailField.classList.add("invalid");
    emailField.value = "PLEASE ENTER VALID EMAIL ADDRESS";
	formValid = false;
  } else if(email !== confirmation){
    emailField.classList.add("invalid");
	confirmationField.value = "EMAIL ADDRESS DO NOT MATCH";
	formValid = false;
  }
    
  return formValid;
}

var form = document.getElementById("coach_campaign"),
    emailField = document.getElementById("emailAddress"),
    confirmationField = document.getElementById("confirm_email");

form.addEventListener("submit", function(e){
    if(!checkEmail()){
        e.preventDefault();
    }
});

I also updated my demo.