0

I have a form the is trying to validate email. If the email is incorrect the class for the input does not show. Below is the code for the html:

<div class="email-entry desktop-container">
<div id="a"></div>
<form name="form1" action="#">
  <input id="test" type="text" name="text1" placeholder="Email Address" value="" onclick="return ValidateEmail(document.form1.text1)">
  <input type="image"  src="images/icon-arrow.svg" alt="submit">
  <p id="addedText"></p>
</form>

and below I have implemented the following javascript:

function ValidateEmail(inputText) {
  var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
  if (inputText.value.match(mailformat)) {
    alert("You have entered a correct email addresss")
    document.form1.text1.focus();
    return true;
  } else {
    // var img = document.createElement('img');
    // img.src = "/images/icon-error.svg"
    document.getElementById("addedText").innerHTML += "This is the wrong email address";
    document.getElementByClassName("invalidEmail")
    // alert("You have entered an invalid email address!");
    document.form1.text1.focus();
    return false;
  }
}

the css I am trying to activate is the following:

.invalidEmail {
  border: 1px solid red;
  background-image: url("/images/icon-error.svg");
  background-repeat: no-repeat;
  background-position: 75% 25%;
}

At the end of the day I want the an image to appear in the form like this.email validation . The issue is nothing seems to appear.

adiga
  • 34,372
  • 9
  • 61
  • 83
AltBrian
  • 2,392
  • 9
  • 29
  • 58
  • You are trying to do `document.getElementByClassName("invalidEmail")` but there is no element which have the class`invalidEmail`. Also, what are you trying to achieve using this? – Sunil Chaudhary Dec 07 '19 at 15:43
  • I’m trying to activate the class invalidEmail in the input element. – AltBrian Dec 07 '19 at 15:50
  • Is [element.classList.add](https://www.w3schools.com/jsref/prop_element_classlist.asp) what you are looking for? `document.getElementsByClassName("invalidEmail")` will simply get you the list of all the elements with `invalidEmail` class – Sunil Chaudhary Dec 07 '19 at 15:54

1 Answers1

0

To add a class to your input element, you need to use element.classList.add and to remove it you need element.classList.remove. Also, document.getElementsByClassName("invalidEmail") will get you all the elements which contain invalidEmail classname. So it is not required here

Have gone through the repo which you mentioned:

  • CSS specificity is one of the culprit here. Adding !important will work fine now (check border in the snippet) but ideally you should read about it and then fix the css without using !important

  • It seems you are not hosting your project and opening the index.html file directly. Host your project on some server (e.g. localhost) and it should work fine (python -m SimpleHTTPServer is a simple command to help you spawn a local server quickly which you can use but there are others as well).

  • function ValidateEmail was not using proper regex test for email testing. Read here for more details

Have tried to fix it for you

function ValidateEmail(inputText) {
  var mailformat = /^(([^<>()\[\]\\.,;:\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,}))$/;;

  if (mailformat.test(inputText.value)) {
    document.getElementById("addedText").innerHTML = "";
    alert("You have entered a correct email addresss")
    //document.form1.text1.focus();
    document.getElementById("test").classList.remove("invalidEmail")
    return true;
  } else {
    // var img = document.createElement('img');
    // img.src = "/images/icon-error.svg"
    document.getElementById("addedText").innerHTML = "This is the wrong email address";
    document.getElementById("test").classList.add("invalidEmail")
    // alert("You have entered an invalid email address!");
    document.form1.text1.focus();
    return false;
  }
}
.invalidEmail {
  border: 1px solid red !important;
  /*Check here*/
  background-image: url("https://image.flaticon.com/icons/png/128/752/752755.png");
  background-repeat: no-repeat;
  background-position: 75% 25%;
  background-size: 12px 12px;
}
<div class="email-entry desktop-container">
  <div id="a"></div>
  <form name="form1" action="#">
    <input id="test" type="text" name="text1" placeholder="Email Address" value="" onclick="return ValidateEmail(document.form1.text1)">
    <input type="image" src="images/icon-arrow.svg" alt="submit" >
    <p id="addedText"></p>
  </form>
</div>

Hope it helps. Revert for any doubts.

Note: This is how icon image and red boundary is coming for me enter image description here

Sunil Chaudhary
  • 4,481
  • 3
  • 22
  • 41
  • Can you please explain what is not working? Are you not getting the image or something else. I have added image in my answer. Let me know, if you are getting something different than that. Or you are looking for something else which I am not able to understand – Sunil Chaudhary Dec 07 '19 at 16:24
  • Nothing seems to happen no image not border highlighted. Just nothing! below is my github https://github.com/amidabrian51/base-apparel-coming-soon – AltBrian Dec 07 '19 at 16:40
  • Hi! I have updated the answer after going through the github repo. Please go through the answer again and hopefully it should work fine now :-) – Sunil Chaudhary Dec 07 '19 at 16:58
  • Unfortunately this is still not working. There is no icon that shows up in the input field. – AltBrian Dec 07 '19 at 22:47
  • Are you getting red border? – Sunil Chaudhary Dec 07 '19 at 22:51
  • I get the icon and the red border when I click into the input element. Once I type either a correct or invalid email I am not getting a response. – AltBrian Dec 07 '19 at 22:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/203823/discussion-between-sunil-and-altbrian). – Sunil Chaudhary Dec 07 '19 at 22:57