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

225
Views
form to check if names match display display alert message saying whether they match or the dont match. if statment used to check the values if same

Hi guys im trying to make a form which when the user enters two values that are the using an f statement same with an onsubmit event handler. that then shows an alert message if they match. my problem is im not seen onsubmit pop up. or an alert i dont know where im going wrong please help.

function nameCheck(){

    let fname = document.querySelector("#fname").value;
    let fname2= document.querySelector("#fname2").value;

    if (fname1 = = fname2){
       alert("The names match ");

} else if{

    alert("They dont match ");
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
    <title> nameCheck</title>

    <script src="java/nameCheck.js" type="text/javascript"></script>

</head>
<body>
 <form action= "">

    Name: <input type="text" id ="fname" name="fname">
     <br><br>
    RenterName: <input type="text" id ="fname2" name="fname2">


    <br>

    <div class = "buttons">
        <input type="submit" onclick()= "nameCheck()" name = "submit"  value="Submit">

        <input type="reset" value="Reset">

    </div>

 </form>
</body>
</html>

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

0

Where to begin...

  1. there is no onclick()="" attribute for elements, it's onclick=""
  2. you have a space between = =
  3. there is no fname1 variable defined
  4. you don't have a condition after else if
  5. if you are using alert() just for your own debugging purpose, use console.log() instead, it will save you time in a long run.
about 4 years ago · Juan Pablo Isaza Report

0

Here is the fixed code:

function nameCheck(){

    let fname1 = document.querySelector("#fname").value;
    let fname2= document.querySelector("#fname2").value;

    if (fname1 === fname2){
       alert("The names match ");

} else{

    alert("They dont match ");
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
    <title> nameCheck</title>

    <script src="java/nameCheck.js" type="text/javascript"></script>

</head>
<body>
 <form action= "">

    Name: <input type="text" id ="fname" name="fname">
     <br><br>
    RenterName: <input type="text" id ="fname2" name="fname2">


    <br>

    <div class = "buttons">
        


        <input type="reset" value="Reset">
<input type="submit" onclick= "nameCheck()" name = "submit" value = "Submit">
    </div>
 </form>
 
</body>
</html>

Variables

So first, in the if(fname1 = = fname2), and the variable creation, fname and fname1 do not match. Change either one so it matches the other.

if() stuff

This is the equality sign: === and you did = =. The space has to be a equal sign.

Also, the else if() is only when you want multiple if()s. else should be used for this program.

HTML Attribute

The onclick() should be onclick.

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!