how to apply json checking in js?

Asked

Viewed 285 times

0

I am creating a form and I want to validate the person’s CPF as follows, if the person’s CPF is already in the json file the program should give an Alert saying that the user already has registration, however I do not know how to do this authentication?!

 <body id="fundo">
    <form action="autenticacao.json" method="post" name="dados" onsubmit="return validaForm(this);" id="paraForm">

      <div>
        <label for="tx_cpf">CPF:</label>
        <input name="tx_cpf" type="text" id="tx_cpf" OnClick="validaForm" />
        
      </div>
    </form>
  </body>

This is the json file data:

{ "dadosUsi" : {
"tx_cpf" : 123456789123 
}}

My js:

function validaForm(frm) {

  if (tx_cpf.value == "" ) {
    alert("Por favor, CPF invalido.");

    return false;
  }
 
  return true;
}

  • Put your function validates form as well

  • ready, put the function

  • This should be done on the server, not in the browser via js.

  • Php would be better then?

  • Whether php would be better or not is already outside the scope, getting into preference languages... So you can use any one that makes the server side, be it Nodejs, PHP etc...

  • You can check with Ajax, do not need a form for this, make a request for the json file and see if what is typed in the input already exists in the file.

Show 1 more comment

1 answer

0


There are a number of inconsistencies in your code and I will quote them in the course of the answer, let’s start with html, you are doing an action for a file json Is that right? There’s no point in you doing this in view of the JSON receives no requests GET, POST, PUT or DELETE etc... The JSON is like a simple text file for organizing structures, does not issue or receive requests. In view of this you call validaForm 2 times, one inside the input when the user clicks and the other in the submission form, the one of the submission is even acceptable, but the one of the click no input, because whenever the user first clicks this field will be empty. Add a button instead by changing this HTML

let cpfs = '{"dadosUsi":[{"cpf":"400985123"},{"cpf":"400985124"},{"cpf":"400985125"},{"cpf":"400985126"}]}'

function validaForm() {
  let cpf = document.getElementById("tx_cpf").value;;
  
  if (cpf.trim().length === 0 ) {
    alert("Por favor, CPF invalido.");
  }
  
  alert(JSON.parse(cpfs).dadosUsi.filter(i => i.cpf === cpf).length > 0 ? "Já tem cadastro" : "Não tem cadastro") 
  
 
  return false
}

 
   

<body id="fundo">
    <form action="" method="post" name="dados" onSubmit="return validaForm()" id="paraForm"> 
        <div>
        <label for="tx_cpf">CPF:</label>
        <input name="tx_cpf" type="text" id="tx_cpf" />
        <button>Checa Cpf</button>
          
       </div>
    </form>
 </body>

Note also that it makes no sense for you to read a file through the client because not always it can be there, not to mention thinking about a system that would be used by other computers, it would be totally meaningless for you to save the data of these users on each client’s computer, this should be done on the server side but I made a demonstration of how it would be possible to check a JSON whether or not there is Cpf, this could be done using the function filter javascript, as its purpose was not defined, I made some changes always returning false in the unsubscribe function, but it is not the best way, you were also checking if Cpf was empty without even having taken the value of it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.