Display my error on blur input field

I have an input field and i want to display and error message when they dont have an input field. i have manage to do that but not able to add my style to it. I want the error message style to appear when the user dont have an input field when leaves it empty.
here is my html markup

<div class="form_label">
            <label>Name</label>
            <input type="text" placeholder="Name" name="name" id="name">
            <span id="error_name"></span>
          </div>

here is my style

#error_name{
  color: red;
  background-color: #f4f2f2;
  border-left: 3px solid #fc6a6a;
  padding: 10px;
  position: relative;
  top: 15px;
  border-top-right-radius: 5px;
  border-bottom-right-radius: 5px;
  font-family: 'Roboto', sans-serif;
}

here is my vanilla javascript code.

function validateName(){
  var elName = document.getElementById('name').value;
  var elErrorName = document.getElementById('error_name');
  var regexNameFormat = '^[a-zA-Z\s, ]+$'; //regex format for name fullname and lastname with space
  if( elName == "" || elName == null ){
    elErrorName.innerText = "Please Provide Your Full Name and Last Name";
    return false;
    }if( elName.match(regexNameFormat)){
      elErrorName.innerHTML = '';
    }else{
      elErrorName.innerText = "Valid Name and Last Name";
      elErrorName.style.color = 'green';
      return false;
    }
  return true;
};
document.getElementById('name').addEventListener('blur', function(){
  validateName();
});

here is the screenshot of what i want…

What part is giving you trouble? I tried pasting your above code into JSBin. I noticed that your logic right now might be backward fro the second if and the else.

These are the results I’m seeing:

  • leave the input empty: get the error
    image
  • put in a name: no error
    image
  • put in non-name characters: get a green message
    image

Hi Ariel, when the user does blur out of focus should have the red warning when puts something should have the green as ok. for now just want that to happen. like if i have empty show red have something cuts the style leave part of the style.

If the part that you are trying to fix is the green text when the user enters something, then you need to change your if / else as I mentioned. The other issue that you might notice is that once you have changed the text to green, it doesn’t change back. For example: if the user enters something in the input and blurs it will give the green ok message. If the user then deletes the text and blurs again, the error message will show but it will still be green. You can explicitly toggle the text color (see example below), but my advice would be to have a “success” class and a “error” class and toggle between those.
Here is a JSBin with your slightly modified code.

the red tip still there…when i have some content need to get rid off it not hidding the whole style

If you want to hide the whole span element you can use visibility: hidden or display: none. Look into the difference between these two methods.

1 Like

i actually did it already, yes i had a bug on my regext file that is why wont take the whole… style off got it… thank you so much…