Validation if any form input has been changed

Asked

Viewed 1,762 times

-1

I need to check if the form has changed, that is, if any input data has been changed, etc. So that when you do the submit, or leave the page, perform some button click, or the menu be informed to the user that the data has been changed, and that it has not yet been saved. I located this link, but I do not know if it is possible, I wanted that in any click that was not the Ubmit of the page and had made the change the user was notified, to do this I have to call the function in several places, or there is a function that does this general on the page of form ?

  • You could put at least one code you’ve developed?

  • @Joãopedroschmitz in any form i want to do the validation of input

  • If you use the event onchange in all fields, it will give an alert whenever it has been modified. Ai then you validate to show the alert only if the person has not saved. Only to do it the way you said it is very annoying, unless it comes all filled, after all whenever someone tampers with any input he will accuse the alert. The ideal would be to warn that it was not saved at the time of Submit.

  • @Máttheusspoo then and how can I do it ? in a quick way without having to touch all the fields ?

  • It’s such a simple thing. But without seeing the code it gets complicated to make an example. Make a onsumbit, in prevents the Submit from happening with the e.preventDefault(), check that everything has been saved (that part I don’t know how to do without seeing how you save). if( /*foi salvo */ ) { form.submit() } else { alert('salve todos os campos!') }. If that’s what you want and you still don’t know how to do, put your code and I’ll make an example

1 answer

2


 $('form').on('change paste', 'input, select, textarea', function(){
    $mudou=true;
});


$( "#mybutton" ).click(function() {

if ($mudou==true){

   var msj='Algo mudou no formulário, deseja envia-lo?';
   if (!confirm(msj)) { 
      return false;
   } else {
     $( "#form_name" ).submit();
   }
}  
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form name="form_name" id="form_name">
  <input type="text" name="a" id="a" value="a" />
  <input type="text" name="b" id="b" value="b" />
  <input type="text" name="c" id="c" value="c" />
  <input type="text" name="d" id="d" value="d" />
  <input type="text" name="e" id="f" value="e" />
  <input type="text" name="f" id="e" value="" />
  <select name="two_g">
    <option value="111">111</option>
    <option value="222">222</option>
    <option value="333">333</option>
  </select>
</form>
<button id="mybutton">submit</button>

  • That way, whenever it changes it appears, I just want an Alert to appear when it tries to exit the current form.

  • 1

    @marianac_costa I’m not sure I understand you eu queria que em qualquer click que não fosse o submit da página e tivesse feito a alteração o usuário fosse notificado

  • For example it changes something in the description, and it has the save button, the back button, and the menu items, etc., I would like any click that was not on the save button, it check and ask the question if you would like to leave without saving.

  • @marianac_costa was like I said, what you are specifying is not what you want, or you want something very unviable. Any click off the save button means even if the person clicks on another input, or whenever they fill in some input, since it will change from empty to filled in.

  • @marianac_costa, I edited the answer. Here we understand better a well formatted code than a thousand words!!

Browser other questions tagged

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