Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

102
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda