How to get data from an input and add the properties of a Javascript function?

2

I am most certainly personal. I would like to take data from my input and put it inside the properties of a function. If the function could be performed (capture all the data) I would like to display a success message, already in case of error an error message. I'm trying to do the function however, I can not know if it's taking the inputs correctly or not.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Cadastro de usuário</title>
  </head>
 <body>
<h1> Formulário de Cadastro </h1>
<form name="form">
  <input type="text" name="name" placeholder=" Type your name." maxlength="30"><br><br>
  <input type="number" name="age" placeholder=" Type your age." min="18" maxlength="65"><br><br>
  <input type="email" name="email" placeholder="Type your email."><br><br>
  <input type="password" name="password" placeholder="Type your password."><br><br>
  <input type="button" name="button" value="Cadastrar" onclick="return validarUser()">
  <script language="javascript" src="./scripts.js"></script>
</form>

Javascript code

function validarUser(params){
  var name = form.name.value,
      age = form.age.value,
      email = form.email.value,
      password = form.password.value;
    return true;
    if (validarUser() == true) {
      console.log("Cadastro realizado com sucesso.");
    } else {
      console.log(" Erro ao fazer o cadastro");
    }
}
    
asked by anonymous 27.02.2018 / 20:54

1 answer

1

It seems like there is something wrong with your code. You are giving return true without at least doing no verification. You are calling the validarUser function within itself, causing a stack overflow . Another problem is that to call the function inside the onclick it is necessary to use parentheses onclick="return validarUser()"

Try to do more or less like this:

function validarUser(){
    
    // você recupera as informações do formulário
    var name = form.name.value,
        age = form.age.value,
        email = form.email.value,
        password = form.password.value;
    
    // verifica cada variável para ver se tem conteúdo ou não
    if (name && age && email && password) {
      console.log("Cadastro realizado com sucesso.");
    } else {
      console.log(" Erro ao fazer o cadastro");
    }
    
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Cadastro de usuário</title>
  </head>
 <body>
<h1> Formulário de Cadastro </h1>
<form name="form">
  <input type="text" name="name" placeholder=" Type your name." maxlength="30"><br><br>
  <input type="number" name="age" placeholder=" Type your age." min="18" maxlength="65"><br><br>
  <input type="email" name="email" placeholder="Type your email."><br><br>
  <input type="password" name="password" placeholder="Type your password."><br><br>
  <input type="button" name="button" value="Cadastrar" onclick="return validarUser()">
  <script language="javascript" src="./scripts.js"></script>
</form>
    
27.02.2018 / 21:09