0

I have this javascript code:

function validate()
{

    var email=document.getElementById('email').value;


    var emailRegex=/^[a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,6}$/;
    var emailResult=emailRegex.test(email);
    alert("email:" +emailResult);
}

and the html part, on a form tag:

<input type="text" id="email" placeholder="EMAIL"><br><br>
<input type="submit" value="REGISTER" id="butt">

I want to print a message everytime that the test of Regex returns false,but I don't want to use an alert box. How can I do this?

Form :

         <form id="registration" onsubmit="validate()">
                <h3>S'ENREGISTRER</h3>
                <label for="button">FULL NAME: </label>
                <small>*</small>
                <input type="text" name="fname" id="fullname" placeholder="FULL NAME"><br><br>
                <label for="button">LAST NAME:</label>
                <small>*</small>
                <input type="text" name="lname" id="lastname" placeholder="LAST NAME"><br><br>
                <label for="button">USERNAME:</label>
                <small>*</small>
                <input type="text" name="uname" id="username" placeholder="USERNAME"><br><br>
                <label for="button">PASSWORD:</label>
                <small>*</small>
                <input type="password" name="pass" id="password" placeholder="PASSWORD"><br><br>
                <label id = "kjo" for="button">EMAIL:</label>
                <small>*</small>
                <input type="text" id="email" placeholder="EMAIL"><br><br>
                <input type="submit" value="REGISTER" id="butt" onclick="validate()">
                <p id="result"></p>
                <br><br>
                <a href="login.html">LOGIN</a>
            </form>
Amelia
  • 1
  • 2
  • you mean console.log() to print out the test of Regex? or you want the users also can see it? If so, you can set a

    element tag and use .innerHtml to display the result in

    tag

    – Elvis Wong May 26 '18 at 15:36
  • 1
    Possible duplicate of [Validate html text input as it's typed](https://stackoverflow.com/questions/9205164/validate-html-text-input-as-its-typed) – William Perron May 26 '18 at 15:37

2 Answers2

1

One possibility would be to add a p tag (or div, span and so on), which serves as a "field" to show the result of the test.

In this case, I'm using a p tag. To print the result, I use innerText like this:

function validate() {

  var email = document.getElementById('email').value;


  var emailRegex = /^[a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,6}$/;
  var emailResult = emailRegex.test(email);
  //alert("email:" + emailResult);
  // Show result withing the added p tag
  document.getElementById('result').innerText = "email: " + emailResult;
}
<input type="text" id="email" placeholder="EMAIL"><br><br>
<input type="submit" value="REGISTER" id="butt" onclick="validate()">
<p id="result"></p>

PS: I also added the onclick attribute to your button, to fire the function when the button get's clicked.


Edit:

If you want to display the error message, after the form gets submitted, you might want to take a look at PHP. With JavaScript on the client side (what we are doing here), you can't achieve that.

One way to get around this problem, would be to prevent the form from submitting if the email is invalid:

function validate(event) { // Mind the "event" parameter here

  var email = document.getElementById('email').value;


  var emailRegex = /^[a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,6}$/;
  var emailResult = emailRegex.test(email);
  //alert("email:" + emailResult);

  // Check if the email is invalid. If not, dont' submit the form
  if (emailResult == false) {
    event.preventDefault(); // This line prevents the form from submitting
    document.getElementById('result').innerText = "email: " + emailResult;
  }
}
<form id="registration" onsubmit="validate(event)"> <!-- mind the "event" parameter here -->
  <h3>S'ENREGISTRER</h3> 
  <label for="button">FULL NAME: </label>
  <small>*</small>
  <input type="text" name="fname" id="fullname" placeholder="FULL NAME"><br><br>
  <label for="button">LAST NAME:</label>
  <small>*</small>
  <input type="text" name="lname" id="lastname" placeholder="LAST NAME"><br><br>
  <label for="button">USERNAME:</label>
  <small>*</small>
  <input type="text" name="uname" id="username" placeholder="USERNAME"><br><br>
  <label for="button">PASSWORD:</label>
  <small>*</small>
  <input type="password" name="pass" id="password" placeholder="PASSWORD"><br><br>
  <label id="kjo" for="button">EMAIL:</label>
  <small>*</small>
  <input type="text" id="email" placeholder="EMAIL"><br><br>
  <input type="submit" value="REGISTER" id="butt"> <!-- I removed the onclick attribute here because it's already in the form tag -->
  <p id="result"></p>
  <br><br>
  <a href="login.html">LOGIN</a>
</form>
<p id="result"></p>
CodeF0x
  • 2,624
  • 6
  • 17
  • 28
  • Thank you so much. I tried it but I'm having a problem. In fact when I click on the 'submit' button (after the input of an invalid email) the message is printed but It disappears. How is this possible? – Amelia May 26 '18 at 15:55
  • @Amelia A form usually redirects you to a page you specify with its `action` attribute. Can you add your form to your question? – CodeF0x May 26 '18 at 15:59
  • I just added it – Amelia May 26 '18 at 16:04
  • @Amelia Thank you. So you want to prevent the form from submitting when the email test is false? – CodeF0x May 26 '18 at 16:11
  • If the email test returns false I want to print an error message after the form,but I want the message to stay and to give the user the opporunity to write again the email – Amelia May 26 '18 at 16:16
0

As the previous answer says, you can insert a tag to show the result, after each field you want to validate.

For email as you mentioned in question, I write the part of showing error message as below.

Element.prototype.remove = function() {
  this.parentElement.removeChild(this);
};
NodeList.prototype.remove = HTMLCollection.prototype.remove = function() {
  for (var i = this.length - 1; i >= 0; i--) {
    if (this[i] && this[i].parentElement) {
      this[i].parentElement.removeChild(this[i]);
    }
  }
};

function insertAfter(newNode, referenceNode) {
  referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}

function validate(event) {
  var emailInput = document.getElementById("email");
  var email = emailInput.value;

  var emailRegex = /^[a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,6}$/;
  var emailResult = emailRegex.test(email);
  if (!emailResult) {
    var errorDiv = document.getElementById("error");
    if (!errorDiv) {
      var div = document.createElement("div");
      div.innerHTML = "Insert a valid Email";
      div.classList.add("error");
      div.id = "error";
      insertAfter(div, emailInput);
      emailInput.classList.add("error-input");
    }
    event.preventDefault();
  } else {
    var errorDiv = document.getElementById("error");
    if (errorDiv) {
      errorDiv.remove();
      emailInput.classList.remove("error-input");
    }
  }
}
.error {
  color: red;
}

.error-input {
  border: 1px solid red;
  border-radius: 5px;
  padding: 5px
}
<form id="registration" onsubmit="validate()">
  <h3>S'ENREGISTRER</h3>
  <label for="button">FULL NAME: </label>
  <small>*</small>
  <input type="text" name="fname" id="fullname" placeholder="FULL NAME"><br><br>
  <label for="button">LAST NAME:</label>
  <small>*</small>
  <input type="text" name="lname" id="lastname" placeholder="LAST NAME"><br><br>
  <label for="button">USERNAME:</label>
  <small>*</small>
  <input type="text" name="uname" id="username" placeholder="USERNAME"><br><br>
  <label for="button">PASSWORD:</label>
  <small>*</small>
  <input type="password" name="pass" id="password" placeholder="PASSWORD"><br><br>
  <label id="kjo" for="button">EMAIL:</label>
  <small>*</small>
  <input type="text" id="email" placeholder="EMAIL"><br><br>
  <input type="submit" value="REGISTER" id="butt" onclick="validate(event)">
  <p id="result"></p>
  <br><br>
  <a href="login.html">LOGIN</a>

</form>

You can also write an event listener onchange or onmouseout or onfocusout for your fields for validating them immediately after each change.

Hope this helps.

Saeed
  • 5,413
  • 3
  • 26
  • 40
  • thank you so much. I just have one question,in this line : var errorDiv = document.getElementById("error"); where do you get the 'error' id cuz it doesn't exist in the html code? – Amelia May 26 '18 at 21:56
  • We add that div dynamically and add `error` id with this loc `div.id = "error";`. Then I check the existence of that id for adding new one or not! @Amelia – Saeed May 27 '18 at 04:25
  • Yes thank you so so much!! Can i ask you another question please? – Amelia May 27 '18 at 15:04
  • Yeah, ask if it is a short question @Amelia – Saeed May 27 '18 at 15:05
  • How to link my html register form to a python script? – Amelia May 27 '18 at 16:13
  • Your server side is python?? @Amelia – Saeed May 27 '18 at 16:14
  • Yes my server sude is python – Amelia May 27 '18 at 16:15
  • I dont know anything about python! google it. maybe these links help you https://docs.python.org/2/howto/webservers.html and http://interactivepython.org/runestone/static/webfundamentals/CGI/forms.html@Amelia – Saeed May 27 '18 at 16:16