Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

217
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda