Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
How can we integrate a "valid email" system in this code?

So now I have my successful code. But what I want to do is include this in my AJAX. So this is my AJAX:

function checkEmail() {
  // var myForm = $("#mainForm").serialize();
  var fname = $("#first").val();
  var lname = $("#second").val();
  var email = $("#email").val();
  var password = $("#password").val();
  var repass = $("#en").val();
  if (fname && lname && email && password && repass && password.length >= 6 && password == repass)) {
    jQuery.ajax({
      url: "connection.php",
      data: {
          fname:fname,
          lname:lname,
          email:email,
          password:password,
          repass:repass
      },
      type: "POST",
      success:function(data){
  $("#emailExists").show();
  $("#email").css("border","2px solid green");
  $("#no").css("visibility","hidden");
  $("#yes").css("visibility","visible");
  if(data){
    $("#email").css("border", "2px solid red");
    $("#no").css("visibility","visible");
    $("#yes").css("visibility","hidden");
    }else
    {
    $("#email").css("border", "2px solid green");
    $("#no").css("visibility","hidden");
    $("#yes").css("visibility","visible");
    window.location.href = 'home.php';
    }
  $("#emailExists").html(data);
  },
  error:function (){
  }
  });
   }
}

So, what I want to do, is basically, in that if statement [if(name && lname...)]. In that particular section, I want to include this particular checking if email valid system too. So I was thinking maybe make this code (the if statement to check if email is valid), into a function, to then add it into the AJAX, so something like this:

  if (fname && lname && email && password && repass && password.length >= 6 && password == repass && checkValidateEmail()) {

But if I keep that if statement in a function called checkValiateEmail() and do that, it isn't working. What should I do?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your error is in line 20 (of my snippet, see below). You are passing your email HTMLElement to the validateEmail() function, not the inputs value. The correct code is the following, you had validateEmail(email).

if(email.value === ""){
    // ...
}
else if (!validateEmail(email.value)){ // <- the error was here
    // ...
}
else{
    // ...
}

The full working code is then:

function validateEmail(email) {
  const 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);
}

const email = document.getElementById("email");
const no = document.getElementById("no");
const yes = document.getElementById("yes");
const mailText = document.getElementById("email-text");
const validEmail = document.getElementById("valid");

email.addEventListener("change", function(){
  if(email.value === "") {
    no.style.visibility = 'visible';
    yes.style.visibility = 'hidden';
    email.style.border = '2px solid red';
    mailText.style.visibility = 'visible';
    mailText.innerText = "Please enter an email address.";
    validEmail.style.visibility = 'hidden';
  } else if (!validateEmail(email.value)) {
    no.style.visibility = 'visible';
    yes.style.visibility = 'hidden';
    email.style.border = '2px solid red';
    mailText.style.visibility = 'hidden';
    validEmail.style.visibility = 'visible';
  } else {
    yes.style.visibility = 'visible';
    no.style.visibility = 'hidden';
    email.style.border = '2px solid green';
    mailText.style.visibility = 'hidden';
  }
});
<input type="text" id="email" />
<span id="yes">Yes</span>
<span id="no">No</span>
<span id="valid">Valid</span>
<p id="email-text"></p>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!