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

103
Vistas
JS problem when creating a Class Function that is gonna validate name by calling function inside the Class

So this is my code: pressing a button creates html mssg: Inside the class Student, I tried to check names for inv characters and empty fields. Then I wanted to pass through another f-on which would check if both f-ons doing their thing and to call her outside the class. I tried different ways to solve it but returns the same result. Please if someone knows to resolve or explain in more details would be grateful.

TY all.

  constructor(fullName, address, phone, course) {
    this._fullName = fullName;
    this.address = address;
    this.phone = phone;
    this.course = course;
  }
  validateName(text) {
    let regex = /^[A-Za-z0-9 ]+$/;
    let isValid = regex.test(text);
    if (!isValid) {
      alert("Contains Special characters");
    } else {
      alert("OK");
    }
  }

  checkName(text) {
    text = text.trim();
    if (text === "") {
      throw new Error("The name can not be empty");
    } else {
      return (this._fullName = text);
    }
  }

  sucess(text) {
    let notChars = this.validateName(text);
    let notEmpty = this.checkName(text);

    if (!notChars || !notEmpty) {
      return alert("chars or empty");
    } else {
      return alert("ok");
    }
  }

  getInfo() {
    return `Name: ${this._fullName} <br> Address: ${this.address}<br> Phone: ${this.phone}<br>  Course: ${this.course}`;
  }
}

let fullName = $("#fullName");
let address1 = $("#inputAddress");
let address2 = $("#inputAddress2");
let inputCity = $("#inputCity");
let inputZip = $("#inputZip");
let phone = $("#phoneNumber");
let show = $("#show");
let course = $("#course");

$("#signInBtn").on("click", (e) => {
  e.preventDefault();
  let address = `${address1.val()} ${address2.val()} ${inputCity.val()} ${inputZip.val()}`;
  let student1 = new Student(
    fullName.val(),
    address,
    phone.val(),
    course.val()
  );

  show.html(student1.sucess("Steva"));
});
about 4 years ago · Juan Pablo Isaza
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