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

127
Vistas
My required field in my form HTML doesn´t work

I have created a in HTML with required fields and then a Submit button with a ng-click=add().

The code is the following:

Enter name:
    <div class="mb-3">
      <label class="form-label">Enter surname:</label>
      <input type="text" class="form-control" placeholder="Person surname" ng-model= "newPerson.surname" required>
    </div>
    
    <div class="mb-3">
      <label class="form-label">Enter age:</label>
      <input type="text" class="form-control" placeholder="Person age" ng-model= "newPerson.age" required>
    </div>

    <div class="mb-3">
      <label class="form-label">Enter occupation:</label>
      <input type="text" class="form-control" placeholder="Person occupation" ng-model= "newPerson.occupation" required>
    </div>
    
    <button type="submit" class="btn btn-primary" ng-click="add()">Add</button>
    
</form>

When pressing the button, it adds the person and then it tells me that the fields are required but in my list the person is already there will undefined values.

I have declared on top of the document as well.

Thanks

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

0

You can as well use a validation method to validate the inputs on submission instead of required. example

public validateForm() {
  if(newPerson.surname === null || 
  newPerson.surname === undefined || newPerson.surname === " ")
  {
    error.surname = "Surname is required";
  }
  if(newPerson.age === null || 
  newPerson.age === undefined || newPerson.age === " ")
  {
    error.age = "Age is required";
  }
  if(newPerson.occupation === null || 
  newPerson.occupation === undefined || newPerson.occupation === " ")
  {
    error.occupation = "Occupation is required";
  }
  
  if(newPerson.surname !== null || 
  newPerson.surname !== undefined || newPerson.surname !== " " ||
  newPerson.age !== null || 
  newPerson.age !== undefined || newPerson.age !== " " ||
  newPerson.occupation !== null || 
  newPerson.occupation !== undefined || newPerson.occupation !== "
  )
  {
    this.add();
  }

}
  <div class="mb-3">
      <label class="form-label">Enter surname:</label>
      <input type="text" class="form-control" placeholder="Person surname" ng-model= "newPerson.surname" required>
    </div>
    <span *ngIf="error.surname"> {{error.surname}}</span>
    <div class="mb-3">
      <label class="form-label">Enter age:</label>
      <input type="text" class="form-control" placeholder="Person age" ng-model= "newPerson.age" required>
    </div>
    <span *ngIf="error.age"> {{error.age}}</span>
    <div class="mb-3">
      <label class="form-label">Enter occupation:</label>
      <input type="text" class="form-control" placeholder="Person occupation" ng-model= "newPerson.occupation" required>
    </div>
    <span *ngIf="error.occupation"> {{error.occupation}}</span>
    <button type="submit" class="btn btn-primary" ng-click="validateForm()">Add</button>
    
</form>

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