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

213
Vistas
I want to add an alert in the if else statement. How do I do that?

I want to add an alert inside the if and else if. If the user does not enter anything in the prompt box the alert triggers. Also if the user enters a number the prompt it will say that the user entered a number. How do do that?

let myForm2 = document.querySelector('.form2');
let pDisplay1 = document.querySelector('.display4');
myForm2.addEventListener('submit', function(e) {
  e.preventDefault();
  let uname = document.querySelector('.inputName2').value;
  if (uname == null) {


  } else if (isNaN(uname) == false) {

  } else {
    pDisplay1.innerHTML = `Welcome to the program ${uname}`;
  }

})
<p> Activity 6</p>
<form class="form2" method="get">
  <label>Full Name: <input type="text" class="inputName2"></label>
  <input type="submit">
</form>
<p class="display4"></p>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

document.querySelector('.className').value will return a string.

string.trim() removes the whitespaces and if the length === 0 it means that the input is empty or has only whitespaces which you generally want to treat as empty. If you consider space is a valid input you don't have to use trim().

The + sign will convert a string into a number otherwise you could use parseInt(variable).

Number.isInteger(variable) will return true if the variable is an integer.

You could also write !isNaN(+uname) or +uname !== Number.NaN

myForm2.addEventListener('submit', function (e) {
    e.preventDefault();
    let uname = document.querySelector('.inputName2').value;
    if (uname.trim().length === 0) {
        alert('You should write something');
    } else if (Number.isInteger(+uname)) {
        alert('You wrote a number');
    } else {
        pDisplay1.innerHTML = `Welcome to the program ${uname}`;
    }
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

Empty string is not equal to null, replace uname==null with uname=='', after the replacement, you can identify the situation that the user did not input, if it is more strict, you can also use trim to remove whitespace and then do condition review

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